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

How do I get to local player with a server script?

I'm trying to make an anti exploit in server script service, because if I did it in a local script, the player could access it, and find a way to avoid detection. But I cannot figure out a way to access him in ServerScriptService.

main = require(script.Parent.Executions.Source) -- A module script

wait(1)

	local name = ?? -- I cannot figure this out!
	local plr = game.Players:WaitForChild(name)
while wait(5) do
	if game.Workspace:WaitForChild(name).Humanoid.WalkSpeed > 16 then
		if not game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, 5473029) -- if he does not have the gamepass then 
		 	if game.Workspace:WaitForChild(name):FindFirstChild("Acceleration Coil") == nil then -- if he does not have a speed coil
					main.kick_Exploiter(name) -- calls the module script to kick player
		end
		elseif game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, 5473029) then -- if the player does have the gamepass, but is still exploiting
			if game.Workspace:FindFirstChild(name).Humanoid.WalkSpeed > 25 then
				main.kick_Exploiter(name) -- calls the module script to kick player
			end
		end
	end
	if game.Workspace:WaitForChild(name).Humanoid.JumpPower > 50 then -- checks the jump power
		if game.Workspace:WaitForChild(name):FindFirstChild("Gravity Coil") == nil then -- if the player does not have a gravity coil
			main.kick_Exploiter(name) -- calls the module script to kick player
		end
	end
end
	

Module script:

module = {}
function module.kick_Exploiter(name)
local plr = game.Players:WaitForChild(name)
plr:Kick("You have been kicked for exploiting, please let this game be fun for other players!")
end
return module

1 comment

0

You can always loop through the children of the Players service.

3 answers

you can use the player parameter of PlayerAdded to get the player on the server. note that PlayerAdded doesnt work on the client.

game.Players.PlayerAdded:Connect(function(player)
	print(player.Name, player.UserId) -- prints the userid and name of the player that joined the game.
end)

also use local variables, as your "main" variable is a global one though its at the top of the scope. prioritize events over while loops and instead indexing the character inside workspace of the name, just use the character parameter of CharacterAdded instead.

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local humanoid = char:WaitForChild("Humanoid")

		humanoid.Health = humanoid.Health - 50
	end)
end)

You can use with PlayerAdded or Touch function

Here is examples:

PlayerAdded

game.Players.PlayerAdded:Connect(function(player) -- On player join
	print("The player " .. player.Name .. " joined in game!") -- Print "The player PLAYER_NAME joined in game"
end)

Touch

script.Parent.Touched:Connect(function(hit) -- On touch in a part
	if hit.Parent:FindFirstChild("Humanoid") then -- Find the Humanoid
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Get player from character
		if player ~= nil then -- Check if player is not nil
			print("I found the player " .. player.Name .. " on touch!") -- print "I found the player PLAYER_NAME on touch"
		end
	end
end)

You can see more is wiki pages:

PlayerAdded - Wiki

Touch - Wiki

Hope it helped :D

Errors? tell-me on comments.

1 comment

0

This helped me a lot, thanks so much! :D

First thing is first, I need to correct your mindsets:

A localplayer is present in localscripts since the localscripts affects the client and not server.

Let's just say the client is the player. The server is a huge box that lets you connect to roblox, the box itself isn't a player.

What helped me understand this is imagining the server as all the players in the game at the moment.

Now with that said, here are ways you can get the player through a server script:

game.Players.PlayerAdded:Connect(function(p)
	local localplayer = p
end)

Typically, events are given parameters, the first parameter can be named whatever you want, and it will be the player IF YOU CHOOSE TO USE THIS.

Similarly, it can be said for this:

BasePart.Touched:Connect(function(obj)
	if obj.Parent:FindFirstChild('Humanoid') then
		local localplayer = game.Players:FindFirstChild(obj.Parent.Name)
	end
end)

The only difference is this event's first parameter is the object the BasePart touches as shown with BasePart.Touched

This also applies to Raycasting, GetTouchingParts and its new successor functions.

This is more detailed in case you were still confused from the other answers.

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