First off, you want to ensure that v.Character is accessible, otherwise it'll throw a null member error. When your character dies, trying to access the player's character can return an error as the player is being deleted and respawned into the game.
Something I like to do is use WaitForChild() on the player's name. Your style would work fine until that player died, and then it'd eventually most likely error. In this example, I'll connect a player added event listener and a Character Added event to the newly joining player, to ensure that every time the player loads, his walkspeed will be set accordingly.
Or, on a local script version, I'll simply connect the PlayerAdded event to the local Player.
wait();
local Player = game:GetService("Players").LocalPlayer;
Player.CharacterAdded:connect(function()
local character = Workspace:WaitForChild(Player.Name);
if not character:FindFirstChild("Humanoid") then
return;
end
character.Humanoid.WalkSpeed =
Player.TeamColor == BrickColor.new("Bright orange") and 30 or 16;
end)
wait();
for index, Player in pairs(game:GetService("Players"):GetPlayers()) do
Player.CharacterAdded:connect(function()
local character = Workspace:WaitForChild(Player.Name);
if not character:FindFirstChild("Humanoid") then
return;
end
character.Humanoid.WalkSpeed =
Player.TeamColor == BrickColor.new("Bright orange") and 30 or 16;
end)
end
This should work, please let me know if it doesn't or if there was anything I left out that I didn't catch.
1 comment
It doesnt seem to do anything