Leaderstats door script add on help

I wanted to create this leaderstats door for my game it worked but if I don’t have the number of coins still it opens,which I don’t want it to

So what script should I type to make this work

local click = script.Parent.ClickDetector

local doorOpened = false

local door = script.Parent

click.MouseClick:Connect(function(plr)
	if doorOpened then return end
	
	doorOpened = true
	
	plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value - 2000
	
	door.Transparency = 1
	
	door.CanCollide = false
	
	wait(3)
	
	door.Transparency = 0
	door.CanCollide = true
	
	doorOpened = false
	

end)

I am not able to figure what script can I type to not allow the player inside and not deduct coins

1 Like
local click = script.Parent.ClickDetector
local doorOpened = false
local door = script.Parent

click.MouseClick:Connect(function(plr)
	if doorOpened then return end
	doorOpened = true
	plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value - 2000
	door.Transparency = 1
	door.CanCollide = false
	wait(3)
	door.Transparency = 0
	door.CanCollide = true
	doorOpened = false
end)

Just reformatting for ease of viewing.

Ok sure actually I’m learning scripting so If I need help I approach the DevForum

local click = script.Parent.ClickDetector
local doorOpened = false
local door = script.Parent

click.MouseClick:Connect(function(plr)
	if doorOpened then return end
	doorOpened = true
	local coins = plr:WaitForChild("leaderstats"):WaitForChild("Coins")
	coins.Value -= 2000
	door.Transparency = 1
	door.CanCollide = false
	wait(3)
	door.Transparency = 0
	door.CanCollide = true
	doorOpened = false
end)

Does this work? Needs to be a server script parented to the door part.

So should I add this is on to the normal script or you want me to delete everything and put this in?

Delete everything and replace it with that, I just made a few modifications to the code you provided.

Ok sure let me see if that works and thanks btw

If it doesn’t work try and show, if any appear, any errors in console when testing the game in studio.

Nope this did not work it is still deducting coins even If i don’t have any coins
image
What I want to do is if the player does not have any coins or less than 2000 then it should not allow him inside and not deduct any coins

Oh, I see what you’re issue is now, thanks for providing that screenshot.

local click = script.Parent.ClickDetector
local doorOpened = false
local door = script.Parent

click.MouseClick:Connect(function(plr)
	if doorOpened then return end
	doorOpened = true
	local coins = plr:WaitForChild("leaderstats"):WaitForChild("Coins")
	if coins.Value >= 2000 then
		coins.Value -= 2000
		door.Transparency = 1
		door.CanCollide = false
		wait(3)
		door.Transparency = 0
		door.CanCollide = true
		doorOpened = false
	end
end)

This checks if the coins value is greater than 2000, if it is the coins are deducted and the door is opened for 3 seconds, if the coins value is lower than the coins are not deducted and the door remains closed.

So what I want my script to do is if I have 2000 coins it should allow the player but if the player has none or less then it should not deduct anything and not open the door

Yeah, I understand what you mean now, the above code will achieve that.

Ok thanks sorry for not reading and just posting this above line

Uhm the not allowing thing works but the allowing thing is not working although I have 2000 coins

local click = script.Parent.ClickDetector
local doorOpened = false
local door = script.Parent

click.MouseClick:Connect(function(plr)
	if doorOpened then return end
	doorOpened = true
	local coins = plr:WaitForChild("leaderstats"):WaitForChild("coins")
	if coins.Value >= 2000 then
		coins.Value -= 2000
		door.Transparency = 1
		door.CanCollide = false
		wait(3)
		door.Transparency = 0
		door.CanCollide = true
		doorOpened = false
	end
end)

I forgot to capitalise the c in “Coins” try again now with the new edit.

local click = script.Parent.ClickDetector
local doorOpened = false
local door = script.Parent

click.MouseClick:Connect(function(plr)
	if doorOpened then return end
	doorOpened = true
	local coins = plr:WaitForChild("leaderstats"):WaitForChild("Coins")
	if coins.Value >= 2000 then
		coins.Value -= 2000
		door.Transparency = 1
		door.CanCollide = false
		wait(3)
		door.Transparency = 0
		door.CanCollide = true
		doorOpened = false
	end
end)

Ohh ok thanks again man appreciate it

uhm nope it’s not working no errors too

local click = script.Parent.ClickDetector
local doorOpened = false
local door = script.Parent

click.MouseClick:Connect(function(plr)
	if doorOpened then return end
	doorOpened = true
	local Coins = plr:WaitForChild("leaderstats"):WaitForChild("Coins")
	if Coins.Value >= 2000 then
		Coins.Value -= 2000
		door.Transparency = 1
		door.CanCollide = false
		wait(3)
		door.Transparency = 0
		door.CanCollide = true
		doorOpened = false
	end
end)

It was just the word coins in :WaitForChild("Coins") that needed a capitalised c. Here, use the following code:

local click = script.Parent.ClickDetector
local doorOpened = false
local door = script.Parent

click.MouseClick:Connect(function(plr)
	if doorOpened then return end
	doorOpened = true
	local leaderstats = plr:WaitForChild("leaderstats")
	local coins = leaderstats:WaitForChild("Coins")
	if coins.Value >= 2000 then
		coins.Value -= 2000
		door.Transparency = 1
		door.CanCollide = false
		wait(3)
		door.Transparency = 0
		door.CanCollide = true
		doorOpened = false
	end
end)

uhm nope not working again.What could be the issue

local click = script.Parent.ClickDetector
local doorOpened = false
local door = script.Parent

click.MouseClick:Connect(function(plr)
	if doorOpened then return end
	doorOpened = true
	local coins = plr:WaitForChild("leaderstats"):WaitForChild("Coins")
	print(coins.Value)
	if coins.Value >= 2000 then
		coins.Value -= 2000
		door.Transparency = 1
		door.CanCollide = false
		wait(3)
		door.Transparency = 0
		door.CanCollide = true
		doorOpened = false
	end
end)

Replace everything with this, then test in studio, when the door is clicked the amount of coins you have should be printed to the console. Just to reiterate this must be inside a normal script & not a local script.