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

How do I mention player on a normal script?

Asked by 5 years ago

So I want to mention a player in a normal script... I can't use localplayer like I used until now cuz Im not using a local script, I would love help thx :)

0
In what context do you want to mention a player and when? When a player clicks on a clickdetector? Or when a player walks on a part? There are different ways to approach based on context. noammao 294 — 5y
0
I think he just meant defining the local player. When you define it once, you can use it on other functions. MiguRB 60 — 5y
0
ah true. noammao 294 — 5y

2 answers

Log in to vote
0
Answered by
MiguRB 60
5 years ago
Edited 5 years ago
local plr

game.Players.PlayerAdded:Connect(function(player)
    plr = player
end)

wait(1) -- To wait for the player to add

print(plr.Name)

It sets another variable (plr) to the player that gets defined when the PlayerAddedfunction runs. If you need any further explanation just ask.

Ad
Log in to vote
0
Answered by
noammao 294 Moderation Voter
5 years ago
Edited 5 years ago

This is a way you can get a player from a server-side script if you have the players character.

local Players = game:GetService("Players")

local part = script.Parent

local function onTouched(part)
    local player = Players:GetPlayerFromCharacter(part.Parent) --part.Parent refers to the Character.
    print(player.Name .. " pressed me!")
end

part.Touched:Connect(onTouched)

I don't know what you mean by "mentioning a player". Like, in what context?

Players:GetPlayerFromCharacter()

Answer this question