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

How do you use player in a non-local script?

Asked by 5 years ago
Edited 5 years ago

I know there are some ways like:

for _,plr in pairs(game.Players:GetPlayers()) do

end

--------------------------------
local plr = game:GetService("Players").LocalPlayer -- Never works.

Need it for this script.

local VIP = require(script.Parent);
local debounce = false;
local market = game:GetService("MarketplaceService");
local ID = VIP.infromation.vipID;

VIP.infromation.obj.portal.Touched:Connect(function(hit)
    local player = nil;-- What do I put here>
    if market:UserOwnsGamePassAsync(player.UserId,ID) then
    if not debounce then debounce = true
        if hit.Parent.Character ~= nil then
            hit.Parent.Character:moveTo(VIP.infromation.obj.enter.front)
            wait(1)
            debounce = false
            end
        end
    end
end)
0
use :GetPlayerFromCharacter() User#23365 30 — 5y
0
I changed local player = nil; to local player = hit.Parent:GetPlayerFromCharacter() but it does not work. namespace25 594 — 5y
1
You use GetPlayerFromCharacter on the Players service and place the character inside of the parenthesis. xPolarium 1388 — 5y
0
It would have to be an if conditional statement if you wanted that. Is this script in a part? Then “if hit.Parent:FindFirstChild("Humanoid") then” ABK2017 406 — 5y
0
No you still need to reference game.Players for it to work xxcoordinatorxx 135 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 4 years ago

So there are a variety of ways of doing this, but probably the most common ways are:

1) the use of remote events' FireServer() function and the OnServerEvent() event

--local script
local remote = game.ReplicatedStorage.RemoteEvent
remote:FireServer()
--server script
local remote = game.ReplicatedStorage.RemoteEvent
local player 
remote.OnServerEvent:Connect(function(plr)
    ...
end)

2) the use of the :GetPlayerFromCharacter() function

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

3) the PlayerAdded event

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

4) the PlayerRemoving event

game.Players.PlayerRemoving:Connect(function(player)
    ...
end

5) indexing a player in the list of players

local plrs = game.Players:GetPlayers()
local plr = plrs[math.random(1,#plrs)]
Ad
Log in to vote
0
Answered by 5 years ago

Easy!

Make a Local Script or use RemoteEvents :)

0
I don't think it is a good idea to put a local script in a part and state the Touched event. namespace25 594 — 5y
0
Why not sunny too shy? xxcoordinatorxx 135 — 5y
0
Yes, namespace25 594 — 5y
0
thats a bad idea its better to get the player from the hit Sergiomontani10 236 — 5y

Answer this question