Jump to content

Recommended Posts

Hello, I need help :)! So, my character has some code which makes other players speed increase by 75% when his insane & they're close to him & this the code 

if GLOBAL.TheNet:GetIsMasterSimulation() then

local SPEED_RADIUS = 9.5
local SPEED_MULTIPLIER = 1.75

local function SpeedUp(inst, guy)
	guy.components.locomotor:SetExternalSpeedMultiplier(inst, "sparkspeed", SPEED_MULTIPLIER)
	guy.sparkspeedgiver = inst
end

local function SlowDown(inst, guy)
	guy.components.locomotor:RemoveExternalSpeedMultiplier(inst, "sparkspeed")
	guy.sparkspeedgiver = nil
end

local function UpdateSlowness(inst)
	for i, v in ipairs(GLOBAL.AllPlayers) do
	if v.sparkspeedgiver == inst then
	SlowDown(inst, v)
    end
    end
end

local function CheckAndSpeedUp(inst)
	for i, v in ipairs(GLOBAL.AllPlayers) do
	if v ~= inst then
	if inst:IsNear(v, SPEED_RADIUS) then
	if not v.sparkspeedgiver and not v:HasTag("mosslingwhisperer") then
	SpeedUp(inst, v)
	end
	else
	if v.sparkspeedgiver == inst then
	SlowDown(inst, v)
    end
    end
    end
    end
end

local function ApplyFearSpeed(inst)
	if inst.fearspeedtask == nil then
	inst.fearspeedtask = inst:DoPeriodicTask(1, CheckAndSpeedUp)
    end
end

local function RemoveFearSpeed(inst)
	if inst.fearspeedtask ~= nil then
	inst.fearspeedtask:Cancel()
	inst.fearspeedtask = nil
	end
	UpdateSlowness(inst)
end

local function AddSpeedStimulator(inst)
	inst:ListenForEvent("goinsane", ApplyFearSpeed)
	inst:ListenForEvent("gosane", RemoveFearSpeed)
	inst:ListenForEvent("onremove", UpdateSlowness)
end

AddPrefabPostInit("adam", AddSpeedStimulator)

now the problem is, is I want any character that's within the radius that doesn't have the tag mosslingwhisperer to lose 2.5 points of hunger for every second they are moving within the radius of the speed boost, so I tried changing one of the parts of the code like this but it didn't work :(... So, I need help, please help me :D........

 local function CheckAndSpeedUp(inst)
	for i, v in ipairs(GLOBAL.AllPlayers) do
	if v ~= inst then
	if inst:IsNear(v, SPEED_RADIUS) then
	if not v.sparkspeedgiver and not v:HasTag("mosslingwhisperer") then
	SpeedUp(inst, v)
	elseif not v.sparkspeedgiver and not v:HasTag("mosslingwhisperer") and v:HasTag("sparkspeed") and not v.sg:HasStateTag("moving") then
	SpeedUp(inst, v)
	v.hungerdrain:Cancel()
	elseif not v.sparkspeedgiver and not v:HasTag("mosslingwhisperer") and v.sg:HasStateTag("moving") then
	SpeedUp(inst, v)
	v.hungerdrain = v:DoPeriodicTask(1, function()
	v.components.hunger:DoDelta(-2.5)
	end)
	end
	else
	if v.sparkspeedgiver == inst then
	SlowDown(inst, v)
	elseif v.sparkspeedgiver == inst and v.sg:HasStateTag("moving") then
	SlowDown(inst, v)
	v.hungerdrain:Cancel()
    end
    end
    end
    end
end

Thank you for reading, have a lovely day/night :D!!!

Edited by SuperDavid
local function DrainHunger(guy)
	if guy.components.locomotor:WantsToMoveForward() then
		guy.components.hunger:DoDelta(-2.5)
	end
end

local function SpeedUp(inst, guy)
	guy.components.locomotor:SetExternalSpeedMultiplier(inst, "sparkspeed", SPEED_MULTIPLIER)
	guy.sparkspeedgiver = inst

	if not guy.sparkhungerdrain then
		guy.sparkhungerdrain = guy:DoPeriodicTask(1, DrainHunger)
	end
end

local function SlowDown(inst, guy)
	guy.components.locomotor:RemoveExternalSpeedMultiplier(inst, "sparkspeed")
	guy.sparkspeedgiver = nil

	if guy.sparkhungerdrain then
		guy.sparkhungerdrain:Cancel()
		guy.sparkhungerdrain = nil
	end
end

 

Problem with polling like this is that if the person stops every second then there is no negative hunger, and the radius detection is as accurate as the polling rate.

I'd feel it'd be best to record the position from the prior poll, check if the player is still having the buff, and then apply the negative hunger with respect to the distance traveled.

Then put a maximum to stop edge cases such as wormhole usage with giant distances.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...