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

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

Asked by 6 years ago
Edited 6 years ago

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
0
You can always loop through the children of the Players service. User#25115 0 — 6y

3 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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)
Ad
Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
6 years ago
Edited 6 years ago

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.

0
This helped me a lot, thanks so much! :D Dave_Robertson 42 — 4y
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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.

Answer this question