Using Remote Events to Change Player Speeds Error

I’ve been trying to change a player’s walkspeed by doing a Remote Event (from client to server). I’ve been getting this error, however:

"attempt to index nil with ‘Connect’

I have two scripts; one a localscript in the StarterPlayerScripts folder and a script in the ServerScriptService

Here is the local script:

-- local teamService = game:GetService("Teams");
local toggleSpeedChange; -- Variable for changing player speed when the player has switched to the Spectators team

local player = game.Players.LocalPlayer;
local characterModel = player.Character or player.CharacterAdded:wait();
local character = characterModel:WaitForChild("Humanoid");

local replicatedStorage = game:GetService("ReplicatedStorage");
local changePlayerWalkSpeedEvent = replicatedStorage:WaitForChild("ChangePlayerWalkSpeedEvent");
print("TEST");
for _, team in pairs(teamService:GetTeams()) do
	team.PlayerAdded:connect(function(player) -- Every time a player is added onto a team
		changePlayerWalkSpeedEvent:FireServer(100);
	end)
end

Here is the server script:

-- 
local replicatedStorage = game:GetService("ReplicatedStorage");
local changePlayerWalkSpeedEvent = Instance.new("RemoteEvent", replicatedStorage);
changePlayerWalkSpeedEvent = "ChangePlayerWalkSpeedEvent";

local function onChangePlayerWalkSpeedFired(player, walkSpeed)
	player.Humanoid.WalkSpeed = 100;
end

changePlayerWalkSpeedEvent.OnServerEvent:Connect(onChangePlayerWalkSpeedFired);

I’m also getting a warning message saying:

Infinite yield possible on 'ReplicatedStorage:WaitForChild(“changePlayerWalkSpeedEvent”)

I can’t seem to work around this error message to allow the client to change its walk speed using a Remote Event.

Your problem is that you typed: player.Humanoid
where as in the player there is no such thing as a humanoid

maybe this will help :

local function onChangePlayerWalkSpeedFired(player, walkSpeed)
	player.Character.Humanoid.WalkSpeed = walkSpeed;
end
1 Like

Yep also I think you meant to change the name of the remote event instance and not turn it into a string variable.

1 Like

Gotcha. I’m still getting an error message however.

Oof. That is where I went wrong. I spent a good hour re-writing and erasing code and it was this the whole entire time. >_<

you forget to type .Name

changePlayerWalkSpeedEvent.Name = "ChangePlayerWalkSpeedEvent";

2 Likes

Small tip, change WalkSpeed = 100 to WalkSpeed = walkSpeed

1 Like

There is no point in changing a player’s speed on the server since if you change it on the client, you’ll still get the results you want. Physics replicate to the server ).

Even if you want to for some reason, the problem is that you don’t index character in player.

local function onChangePlayerWalkSpeedFired(player, walkSpeed)
	player.Character.Humanoid.WalkSpeed = 100;
end

For your yield error, changePlayerWalkSpeedEvent.Name = "ChangePlayerWalkSpeedEvent";