StarterPlayerScripts, I am trying to make a script that makes a door able to be walked through when you have 20+ points

The script below does not work for me, I have given myself the correct amount of points in order to unlock the door, but it does not do anything.

local player = game.Players.LocalPlayer
local points = player.leaderstats:WaitForChild("Points") --The points leaderstat

local part = workspace:WaitForChild("Unlock Door") --The unlockable door

if points.Value >= 20 then
	part.CanCollide = false
	part.Transparency = 0.5
	part.Anchored = true
else
	part.CanCollide = true
	part.Transparency = 0
	part.Anchored = false
end

No errors, tried doing stuff like putting the script in the part, that doesn’t work.
Thanks!

You only check if the player has more than 20 points once

local player = game.Players.LocalPlayer
local points = player.leaderstats:WaitForChild("Points") --The points leaderstat

local part = workspace:WaitForChild("Unlock Door") --The unlockable door
points.Changed:Connect(function()
if points.Value >= 20 then
	part.CanCollide = false
	part.Transparency = 0.5
	part.Anchored = true
else
	part.CanCollide = true
	part.Transparency = 0
	part.Anchored = false
end
end)

1 Like

You should use either the Changed event or GetPropertyChangedSignal event to detect any change of the value, because as what @CZXPEK said above, the code you have given will only run once.

Here is your updated script (I have optimized your code a little bit):

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

local leaderstats = player:WaitForChild("leaderstats")
local points = leaderstats:WaitForChild("Points") --The points leaderstat

local part = workspace:WaitForChild("Unlock Door") --The unlockable door

if points.Value >= 20 then -- this statement will run as soon as the player joins
	part.CanCollide = false
	part.Transparency = 0.5
	part.Anchored = true
else
	part.CanCollide = true
	part.Transparency = 0
	part.Anchored = false
end

points:GetPropertyChangedSignal("Value"):Connect(function() -- detects any changes to the 'Points' Value
	if points.Value >= 20 then
		part.CanCollide = false
		part.Transparency = 0.5
		part.Anchored = true
	else
		part.CanCollide = true
		part.Transparency = 0
		part.Anchored = false
	end
end)
2 Likes

Code works, and thanks for explaining it as well!
Marking yours as the solution, but @CZXPEK was also correct.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.