Getting the player

Hello, So I’m currently trying to get the local player in a normal script. Normally you can do but Is it possible to make a variable to get the player without having to use a function? or a remote event?

Suggestions are much appreciated!

script.Parent.Parent.Touched:Connect(function(player)

print(Player.Name)

end

Kind Regards ZoomCode.

16 Likes

via a touched even in a normal script, you have to use the GetPlayerFromCharacter Function:

thing.Touched:Connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
end)

As if a player touched a part, the part(s) of the player touching the part are the parts picked up by the touched event that part, as it is a bodypart, would be a Child of the Player’s Character

14 Likes

Use this thread for this solution;

1 Like

To do this:

script.Parent.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
       local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
       -- blah blah blah
    else
        -- no humanoid found
        --blah blah blah
    end
end)

This is my way of doing it, I guess.

3 Likes

The problem is i don’t want to get the player when they hit something I want it to be a variable I can use anywhere in the script.

2 Likes

The problem is i don’t want to get the player when they hit something I want it to be a variable I can use anywhere in the script.

Hiya. So I looked at the thread but I"m slightly confused. I’m trying to get the player with a variable like this
local Player = (“What command Can I put here to get the player?”)

So that I can call it threw out the script.

You don’t need to check for the humanoid before retrieving the player. This code works the same as your code:

script.Parent.Parent.Touched:Connect(function(hit)
       local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
       if plr then
              -- player exists
       else
              -- player does not exist
       end
end)

@ZoomCode The code above will get the player (plr) when script.Parent.Parent is touched.

Scripts run on the server. LocalScripts run on the client (each player). You can’t really get the local player to the server, as which one would you pick?

In a LocalScript, game.Players.LocalPlayer

In a Script, game.Players.ThePlayerYouWant

1 Like

Right but that’ll only run when they touch the part correct?

Yes. It will only run when they touch the part.

Well, that really depends on what you are trying to do, are you trying to get all players, are you trying to get a player if they have specifications, or are you just trying to get a specific player because you want that player and not any other player

I’m just trying to get one player

I want to change things in their backpack and in the StarterGui So I’m trying to get the local player.

Right but I"m trying to get a variable.

The problem is that the server doesn’t have the concept of a local player, its like telling a parent to get their “local kid” or a teacher to get their “local student”

It wouldn’t make any sense, and they would be very confused by it

Are you trying to make the change on all Players’ backpacks, or are you trying to change it on a condition?

I’m trying to change it on a condition

what exactly is that condition?

Status = script.Parent

local Player =

Status.Changed:Connect(function()
if Status.Value == 1 then
print(Player.Name)
end
end

if its being done from a local script then you just do this:

local player = game.Players.LocalPlayer

else if your doing it on a server script you can use the playerAdded event

game.Players.PlayerAdded:Connect(function(player--[[this is the player variable]])
    Status = script.Parent
    Status.Changed:Connect(function()
        if Status.Value == 1 then
            print(Player.Name)
        end
    end
end)

but could you explain what do you need it on for a server script because there is most likely a far better solution of doing this.

1 Like