Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I access the physical player using a regular script?

Asked by 4 years ago

I need to be able to access the physical player to change the humanoid of certain players without having them step on or click anything. I don't think it would work with a player added function either because it needs to happen in the middle of the game, but I could be wrong about this. I have tried to get the player name using local player, however that didn't work.

local players = game:GetService("Players")

while wait(1) do
    for i,v in pairs(players:GetPlayers()) do
        game.Workspace(v.Name).Humanoid.WalkSpeed = 20
    end
end

When using the script above this error message comes up: - Workspace.SeekingA:5: attempt to call a userdata value

And when using the script below it comes up with the same error message.

local players = game:GetService("Players")

while wait(1) do
    for i,v in pairs(players:GetPlayers()) do
        game.Workspace(players.Name).Humanoid.WalkSpeed = 20
    end
end

2 answers

Log in to vote
0
Answered by 4 years ago

The problem is that you wrote down players.Name. You should've done v.Name as "v" is equal to the player. Also, I would use a PlayerAdded event followed by a CharacterAdded event as it's more simple. Try this:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character.Humanoid.WalkSpeed = 20
    end)
end)

First, the PlayerAdded event fires when, well, a player gets added. Then the CharacterAdded event fires when the character of the player gets added. Then it sets the player's humanoid's WalkSpeed to 20. Also, instead of using a script, you can just go to the StarterPlayer service and set the default WalkSpeed to 20 in it's properties.

Ad
Log in to vote
0
Answered by
Mayk728 855 Moderation Voter
4 years ago
Edited 4 years ago

There are many different ways to do this, it just depends on what you're doing. Also, you cannot use game.Players.LocalPlayer in a server script, that only works in LocalScripts.

I fixed your script here:

local players = game:GetService("Players")

while wait(1) do
    for i,v in pairs(players:GetPlayers()) do
        if v.Character and v.Character:FindFirstChild('Humanoid') then
            v.Character.Humanoid.WalkSpeed = 20
        end
    end
end

You can also use this method, which will set a players WalkSpeed each time they respawn.

local players = game:GetService('Players')

players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        repeat wait() until char and char:FindFirstChild('Humanoid')
        char.Humanoid.WalkSpeed = 20
    end)
end)

Answer this question