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 make it so only players that are under or equal to 3 can enter?

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)

3 comments

0

So this script is located under the part that is the "door"?

0

it's located in the workspace and how to I make it so it finds to number of rebirths?

0

One thing tho! You cannot use game.Players.LocalPlayer in a normal script! You can only use this in a localscript.

2 answers

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)

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)

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