Change WalkSpeed with Remote Event

Hello everyone, I am currently trying to make an energy drink that changes the players WalkSpeed but im having troubles actually changing the WalkSpeed when the remote event is fired.

I have a local script in the drink itself which fires the event;

local Bottle = script.Parent

local slurp = Bottle.slurp

local anim = Bottle.DrinkAnim

local Drank = false

script.Parent.Activated:Connect(function()
if Drank then
return
end

local LocalPlayer = game:GetService("Players").LocalPlayer
local toolCount = LocalPlayer:FindFirstChild("EnergyDrinkCount")

Drank = true

local char = Bottle.Parent
local hum = LocalPlayer.Character.Humanoid
local animtrack = hum:LoadAnimation(anim)
	
animtrack:Play()
Bottle.Lid:Destroy()
slurp:Play()

wait(1)

local function changeWalkSpeed(newWalkSpeed) 
	game.ReplicatedStorage.Energy:FireServer(newWalkSpeed)
	print("fired")
end

changeWalkSpeed(30) -- call function to fire event and change walkspeed

game.ReplicatedStorage.EnergyDrinkAmnt:FireServer()
script.Parent:Destroy()	

end)

This script is located in the ServerScriptStorage and handles actually changing the walkspeed and recieve the fired event;

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)

local EnergyEvent = ReplicatedStorage.Energy

EnergyEvent.OnServerEvent:Connect(function(Player, newWalkSpeed)
if Player:IsA(“Player”) and type(newWalkSpeed) == “number” then
local character = Player.Character
local Humanoid = character and character:FindFirstChild(“Humanoid”)

	if Humanoid then
		Humanoid.WalkSpeed = newWalkSpeed
		print("walkspeed changed")
		
		wait(10)
		
		Humanoid.WalkSpeed = 16 -- revert the walkspeed after 10 seconds
	end
end

end)

any suggestions?

Well, first of all, there’s a major security hole in your code. You are allowing the client to tell the server what the walkspeed should be. A better solution would be to convert the local script into a server script and when the player activates the tool, change the walkspeed on the server and use a remote event to change it on the client. Something like this:

local playerService = game:GetService("Players")

local Bottle = script.Parent
local Drank = false
local newWalkSpeed = 30
local slurp = Bottle.slurp
local cooldown = 2
local powerupDuration = 10
local defaultWalkSpeed = game.StarterPlayer.CharacterWalkSpeed

script.Parent.Activated:Connect(function()
	if Drank then
		return
	end

	local char = script.Parent.Parent
	player = playerService:GetPlayerFromCharacter(char)
	local toolCount = player:FindFirstChild("EnergyDrinkCount")
	if toolCount.Value == 0 then
		return
	end

	local human = char:WaitForChild("Humanoid")
	Drank = true

	game.ReplicatedStorage.EnergyDrinkAmnt:FireClient(player, true, newWalkSpeed)
	human.WalkSpeed = newWalkSpeed
	local count = toolCount.Value
	toolCount.Value = count - 1
	slurp:Play()
	Bottle.Lid:Destroy()
	delay(cooldown, function()
		Drank = false
	end)
	delay(powerupDuration , function()
		game.ReplicatedStorage.EnergyDrinkAmnt:FireClient(player, false, defaultWalkSpeed)
		human.WalkSpeed = defaultWalkSpeed
	end)
end)

And the client script:

local Bottle = script.Parent
local anim = Bottle.DrinkAnim
local localPlayer = game.Players.LocalPlayer
local char = localPlayer.Character
local human = char:WaitForChild("Humanoid")
local animator = human:WaitForChild("Animator")
local animtrack = animator:LoadAnimation(anim)

game.ReplicatedStorage.Energy.OnClientEvent:Connect(function(playEffects, newWalkSpeed)
	if playerEffects == true then
		animtrack:Play()
	end
	human.WalkSpeed = newWalkSpeed
end)

The tool activation is handled by the server. It performs some sanity checks such as debounce (Drank) and checking if the player has a drink to use. If those checks pass, then it continues on to deduct one from the player’s tool count, change the walkspeed, then sends a notification to the client to change the walkspeed. Since there is a true in what is being sent to the client, the client will play the animation, which will replicate to all other clients. As for playing the sound, you can play it on the client, but only that client will hear it. If you play the sound on the server, then all clients will hear it.

I’m sure there’s bugs/typos in this as I just came up with this off the top of my head. But it’s also 3AM where I’m at.

Have Fun.

1 Like

Thank you for taking the time to reply & share the script, I really appreciate that! I did try to implement this but i didn’t have luck and I’m thinking it’s because I have a stamina bar GUI with a sprint script, which after some research I found that can be a cause as to why this script wasn’t changing the walkspeed. So now I’m thinking of implementing into the sprint script itself.