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

How do I make it so only players that are under or equal to 3 can enter?

Asked by 4 years ago

This is the code i tried to use.

local player = game.Players.LocalPlayer
local Players = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
    if Players.LocalPlayer.Character.leaderstats.rebirths.Value <= 3 then
        if plr then
        plr.Character.HumanoidRootPart.CFrame = CFrame.new(1476.7, 0.5, -173.8)
        end
    end
end)

0
So this script is located under the part that is the "door"? rabbi99 714 — 4y
0
it's located in the workspace and how to I make it so it finds to number of rebirths? Tecknobladee 4 — 4y
0
One thing tho! You cannot use game.Players.LocalPlayer in a normal script! You can only use this in a localscript. rabbi99 714 — 4y

2 answers

Log in to vote
0
Answered by
rabbi99 714 Moderation Voter
4 years ago

Your question was vague but I hope this works!

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
        if hit.Parent.leaderstats.rebirths.Value <= 3 then
                if plr then
                    plr.Character.HumanoidRootPart.CFrame = CFrame.new(1476.7, 0.5, -173.8)
                end
        end
    end
end)

Ad
Log in to vote
0
Answered by
lucided 15
4 years ago

You got a few things wrong. One thing you need to remember is that Players:GetPlayerFromCharacter(char) returns the player, and nil if no player is found. You don't need to do anything else other than checking if its valid.

With this, we can detect if the receiving end of the .Touched signal is a player. Like so:

script.Parent.Touched:Connect(function(otherPart)

    -- Attempt to get the player
    local plr = game.Players:GetPlayerFromCharacter(otherPart.Parent)

    -- Check if the player actually exists before doing anything with it - `nil` is falsey
    if plr then

        -- Player exists. Now we can access the leaderstats!
        if plr.leaderstats.rebirths.Value <= 3 then

            -- Leaderstats are under or equal to 3, teleport to wherever you like
            plr.Character.HumanoidRootPart.CFrame = CFrame.new(...)

    end
end)

Answer this question