What is wrong with this door script?

So, for the game that I am working on, I decided to add a cash value to the leaderstats and then make a door that you can walk through if you have enough cash.
I am decently new to programming and was just wondering if anyone had any feedback on this code that would be great, thank you.

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("BoolValue",plr)
	stats.Name = "leaderstats"

	local cash = Instance.new("IntValue",stats)
	cash.Name = "Cash"
	cash.Value = 0
	
	if cash.Value >= 100 then
		game.Workspace.Door.CanCollide = true
	else 
		game.Workspace.Door.CanCollide = false 
	end
	
end)


2 Likes

First of all, you’re setting the value to 0 when they join, then asking if their cash is over 100

1 Like

I see what you’re saying lol. I am just learning right now so I apologize. Thank you!

Also, I added cash value to the player when testing.

1 Like
game.Players.PlayerAdded:Connect(function(plr)
	local _stats = Instance.new("BoolValue", plr)
	_stats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue", _stats)
	cash.Name = "Cash"
	
	workspace.Door.CanCollide = not (cash.Value >= 100)
end)

Make it so it’ll only check the players value when you interact with the door in some way. I’m assuming you already know how to do this, and I’m not much of a scripter myself.

1 Like

If you want the door to be entered, don’t do it in a server sided script since it’ll be for everyone meaning one person can have 100 cash and people will be able to enter it even without 100 cash or above.

For simple terms and instead of the player having to rejoin once they get 100 cash…

You should make it so you add a touched function to the door, when it gets touched, you should check if the player who touched it has 100 cash using players:GetPlayerFromCharacter(chr), make sure it’s in a local script so it’s only seen for the player who has the 100 cash, not everyone who doesn’t have 100 cash.

Or you could just make a function if the character respawned and it would make the door be enter-able by the player.

2 Likes

Thank you for the help, appreciated.

What If an exploiter were to change their value to 100, and just walk through the door? I see a flaw in this, or I’m not understanding this fully.

edit I know local scripts are for the client only, and not to the server. I know exploiters can change their values on the client, but not the server. The server sees 0, but the client sees 100, and they can just bypass that with cheating in the value, and taking advantage of the local script

1 Like

Try to make a bool value named “CanSet”, if the game tries to give you some value, it should be set to true then it’ll give you it then later set to false, if the player gets value without the game turning the bool value to true, they get kicked or banned if you’d like.

1 Like

That sounds like a good idea to combat this issue, and I think it would help the person making this door a lot. I think I get what you mean.

1 Like