Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

5 comments

0

use :GetPlayerFromCharacter()

0

I changed local player = nil; to local player = hit.Parent:GetPlayerFromCharacter() but it does not work.

1

You use GetPlayerFromCharacter on the Players service and place the character inside of the parenthesis.

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”

0

No you still need to reference game.Players for it to work

2 answers

✓ Accepted answer

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)
  1. the use of the :GetPlayerFromCharacter() function
script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr then	
		...
	end
end)
  1. the PlayerAdded event
game.Players.PlayerAdded:Connect(function(player)
	...
end
  1. the PlayerRemoving event
game.Players.PlayerRemoving:Connect(function(player)
	...
end
  1. indexing a player in the list of players
local plrs = game.Players:GetPlayers()
local plr = plrs[math.random(1,#plrs)]

Easy!

Make a Local Script or use RemoteEvents :)

4 comments

0

I don't think it is a good idea to put a local script in a part and state the Touched event.

0

Why not sunny too shy?

0

Yes,

0

thats a bad idea its better to get the player from the hit

This discussion is preserved as it was. New answers, comments, and votes are closed.