How do I make this script better?

Hi! I am currently trying to make a script which changes the character’s humanoid properties (like walkspeed or jumppower) reliably. My goal is to make these changes stackable upon each other, for example 2 walkspeed penalty intvalues stack with each other and subtract the stacked value off of the character’s humanoid walkspeed until one of the intvalues have been removed from the character’s folder.

The code below is how I’ve done it, but it doesn’t work very well. For instance, if the stacked penalties equal to a bigger value than the walkspeed value, the walkspeed value doesn’t go into the negatives. The script will however give back the walkspeed it tried to take away, which will lead to the walkspeed being bigger than it should be.

local Character = script.Parent
local Humanoid = Character.Humanoid
local ActionFolder = Character:WaitForChild("Actions")
local Temp = nil

ActionFolder.ChildAdded:Connect(function(Item)
	wait()
	if Item.Name == "WSPenalty" then
		Humanoid.WalkSpeed = Humanoid.WalkSpeed - Item.Value
		Temp = Item.Value
	elseif Item.Name == "WSBoost" then
		Humanoid.WalkSpeed = Humanoid.WalkSpeed + Item.Value
		Temp = Item.Value
	elseif Item.Name == "NoRotate" then
		Humanoid.AutoRotate = false
	end
end)

ActionFolder.ChildRemoved:Connect(function(Item)
	wait()
	if Item.Name == "WSPenalty" then
		Humanoid.WalkSpeed = Humanoid.WalkSpeed + Temp
	elseif Item.Name == "WSBoost" then
		Humanoid.WalkSpeed = Humanoid.WalkSpeed - Temp
	elseif Item.Name == "NoRotate" then
		Humanoid.AutoRotate = true
	end
end)

Thank you for reading my post!

Making objects is not the best way to do this.

1 Like

Then what do you suggest? Cause I’m unsure as to what to do.

I don’t completely understand the function of your system.
Instead of computing the information in this script, why don’t you do it in the other script that gives the objects to people?

It’s supposed to be an easy way for me to change the player’s walkspeed, without me needing to use the normal “Humanoid.Walkspeed = Humanoid.Walkspeed - 5” method. That way I can control those without it being overly complicated later when I add more content to the game.

You could just make a function to divide the actions for the script, as in:

function PowerUpDown(Player,Amount,PowerName)
   if PowerName == "WalkSpeed" then
   Player.Character.Humanoid.WalkSpeed += Amount --Amount could be - or +
   end 
end
Number+=Addend
Number-=Subtrahend
Number*=Multiplier
Number/=Divisor

can be used instead of

Number = Number+Addend
Number = Number-Subtrahend
Number = Number-Multiplier
Number = Number/Divisor

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.