I'm having trouble checking conditions

Hi, I am making a UI which has an unlockable functionality.

The functionality itself works fine, but I am having trouble properly notifying the user that they do not have enough points to use the UI.

Here is my script:

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

if player:WaitForChild("leaderstats").Points.Value >= 25 then
	script.Parent.MouseButton1Click:Connect(function()
		player.Character:MoveTo(Vector3.new(-7, -29, -6))
		script.Parent.Text = "Teleported!"
		wait(1.5)
		script.Parent.Text = "Teleport"
	end)
else
	script.Parent.Text = "Locked!"
	script.Parent.MouseButton1Click:Connect(function()
		script.Parent.Text = "πŸ”’"
		wait(1.5)
		script.Parent.Text = "Locked!"
	end)
end

points:GetPropertyChangedSignal("Value"):Connect(function()
	if player:WaitForChild("leaderstats").Points.Value >= 25 then
		script.Parent.Text = "Teleport"
		script.Parent.MouseButton1Click:Connect(function()
			player.Character:MoveTo(Vector3.new(-7, -29, -6))
			script.Parent.Text = "Teleported!"
			wait(1.5)
			script.Parent.Text = "Teleport"
		end)
	else
		script.Parent.Text = "Locked!"
		script.Parent.MouseButton1Click:Connect(function()
			script.Parent.Text = "πŸ”’"
			wait(1.5)
			script.Parent.Text = "Locked!"
		end)
	end
end)

I don’t really know how to describe the problem I am having, so I’ll just put a video to show what I am having trouble with:

Yes, I am aware that the UI is horribly misaligned, I’ll fix it later.

Try this:

local player = game.Players.LocalPlayer
local points = player:WaitForChild("leaderstats").Points
local con = nil

if player:WaitForChild("leaderstats").Points.Value >= 25 then
	con { script.Parent.MouseButton1Click:Connect(function()
		player.Character:MoveTo(Vector3.new(-7, -29, -6))
		script.Parent.Text = "Teleported!"
		wait(1.5)
		script.Parent.Text = "Teleport"
	end)
else
	script.Parent.Text = "Locked!"
	con = script.Parent.MouseButton1Click:Connect(function()
		script.Parent.Text = "πŸ”’"
		wait(1.5)
		script.Parent.Text = "Locked!"
	end)
end

points:GetPropertyChangedSignal("Value"):Connect(function()
    if con then
        con:Disconnect()
    end

	if player:WaitForChild("leaderstats").Points.Value >= 25 then
		script.Parent.Text = "Teleport"
		con = script.Parent.MouseButton1Click:Connect(function()
			player.Character:MoveTo(Vector3.new(-7, -29, -6))
			script.Parent.Text = "Teleported!"
			wait(1.5)
			script.Parent.Text = "Teleport"
		end)
	else
		script.Parent.Text = "Locked!"
		con = script.Parent.MouseButton1Click:Connect(function()
			script.Parent.Text = "πŸ”’"
			wait(1.5)
			script.Parent.Text = "Locked!"
		end)
	end
end)

Yep, this works perfectly.

Do you mind explaining how this works?

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