Speed adding or decreasing

Got an issue whenever a player gets there WalkSpeed decreased or increased by a tool or item, other tools that have the same function effect the WalkSpeed on other tools

So for example I made some armor in my game that decreases speed permanently by - 4
Whenever another script or tool do a speed penalty or increase speed like crouching, it would change the armor speed to lets say 16

So what I want to basically do is make a line of code that checks the players speed and puts it to that speed depending on how much speed the player has

2 Likes

Any chance of sharing the script you are talking about? is it server script or local?

1 Like

i have multiple scripts like, crouching, armor, gun reloads etc

i can give you the crouching one

local player = {}--where the decrease and increase speed begin
					if input.stance == "crouch" then
						player.humanoid.HipHeight = 0
						player.humanoid.WalkSpeed = player.humanoid.WalkSpeed + 2
						animation.crouch:Stop(.2)
						input.stance = "stand"
					else
						input.stance = "crouch"
						player.humanoid.HipHeight =-1.5
						player.humanoid.WalkSpeed = player.humanoid.WalkSpeed - 2
						animation.crouch:Play(.2)
					end
				elseif key == Enum.KeyCode.LeftShift then
					if not sprinting then
						sprinting = true
						player.humanoid.WalkSpeed = 16 * (input.stance == "crouch" and 0.8 or 1.25)
					end
				end
			end
		end
	end)
	userinput.InputEnded:connect(function(object, g)
		if not g then
			if object.UserInputType == Enum.UserInputType.Keyboard then
				local key = object.KeyCode
				if key == Enum.KeyCode.C then
					if sprinting then
						sprinting = false
						player.humanoid.WalkSpeed = 16 * (input.stance == "crouch" and 0.4 or 1)
					end
				end
			end
		end
	end)
end

You could just get the current walkspeed and add or subtract from it. You could also create a number attribute to always store the player’s speed.

so basically im trying to figure out how the script can check how much speed the player has already and set it to that speed the player had

1 Like

how could i store the players speed? could i use like an int value or something?

1 Like

It would greatly help if you shared some code, but i do think i understand what youre saying.

You have a script that decreases the players speed, but when they use another tool that speed penalty is ignored and its set back to the default speed. (I hope this is what you mean)

What i would do is check if the player has a speed boost/decrease active or not. If you have one active then you can script the other tools accordingly

EX:

local speedEffects = plr.SpeedEffects --A string value stored in the player.

---tool equip function...

tool.Unequipped:Connect(function()
   if speedEffects.Value == "Penalty" then
         --Set the walkspeed
         humanoid.WalkSpeed = humanoid.WalkSpeed - 4
   end


end)

I hope this helps

This topic is pretty hard to explain, and im very sorry about that lol
so basically

humanoid.WalkSpeed = humanoid.WalkSpeed - 4

i use this line for every code that decreases and increses speed
the issue is if i had armour (which decreases WalkSpeed to 11) and crouched then uncrouched
it would go to like 14 or 13 depending on how much the uncrouching adds
i think the issue is that humanoid walkspeed cant go to the negatives?
a solution im thinking about is the server saving the characters speed and just putting that speed in instead of setting it to 16 or another number

1 Like

im pretty sure a value would fix that, but im not sure about the storing speed thing

With Attributes under the Player. Something like this would work.

--to create the attributes for each player
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
    plr:SetAttribute("Speed", 16) -- 16 could be whatever
    -- the attribute represents a number and does not change anything in game
end)
-- Every time that you wanted to change the walkspeed value you could either:
-- increase value
Player:SetAttribute("Speed", Player:GetAttribute("Speed") + 5) -- whatever the value is
-- decrease value
Player:SetAttribute("Speed", Player:GetAttribute("Speed") - 5 ) -- whatever the value is
--then whenever you wanted to change the walkspeed you could set the walkspeed equal to the attribute
Player.Character.Humanoid.WalkSpeed = Player:GetAttribute("WalkSpeed")
-- attributes are accessible from any script
    plr:SetAttribute("Speed", 16)

is this putting the speed back to 16
because the normal speed can change depending if you wear armour otr not

1 Like

No, the attribute doesn’t affect any part of the player’s walkspeed. The attribute is simply a way to store the data. The idea was that you could use this to store the value and change it, and then set the walkspeed to this. This would help against walkspeed exploiters as you would be able to revert their walkspeed easily instead of changing their already boosted walkspeed.

1 Like

Kind of confused on what the attribute does, is it okay if you explain it a bit?
im not very good at scripting lol

1 Like

An attribute is basically just a way to store a value under an instance. In this case, the attribute is a number stored under each player. The value is accessible from any script and it serves no real purpose other than storing data. You could use a normal Value to store data but it would be much harder to organize than storing it directly under each player.

1 Like

so basically if the walkspeed use to be 16 then change to 11 (this is what my armor does) would it always set it back to 11 if i did something like crouch then uncrouch and also set the attribute to 11

1 Like

You would need to make it so that upon uncrouching, the walkspeed goes back to what you want it. It will never automatically revert unless you create it so that it always checks but that isn’t necessary.

1 Like

so the attribute always set the walkspeed to 16?

1 Like

No, every time you wanted the walkspeed to change you would set the attribute to the desired value. Then you would change the walkspeed to the attributes value and it would change however you wanted it.
If you wanted to make a way for crouching to change walkspeed you woulld do this kind of thing:

if Crouching == true then
    Player:SetAttribute("Speed", Player:GetAttribute("Speed") - 5)
    --this would change the attribute to what it was previously minus 5
end
--to make it so that the walkspeed actually changed with the value you could do this
Player:GetAttributeChangedSignal("Speed"):Connect(function()
    Player.Character.Humanoid.WalkSpeed = Player:GetAttribute("Speed")
end)
1 Like

im gonna send a video of an example, hard to explain in text

1 Like