So, my code is when I press "e" it destroys a model that is called Common Bacon and gives me ten coins. BUT, after that I can press e still and it'll STILL give me 10 coins, I want it to only give me ten coins if the Model "Common Bacon" is still there. Here's my code
01 | local Plr = game.Players.LocalPlayer |
02 | local model = workspace.CommonBacon |
03 | local amountofcoins = 10 |
04 |
05 | Plr:GetMouse().KeyDown:Connect( function (Key) |
06 | if Key = = "e" then |
07 | wait ( 0.5 ) |
08 | model:Destroy() |
09 | Plr.leaderstats.Coins.Value = Plr.leaderstats.Coins.Value + 10 |
10 | end |
11 | end ) |
In Roblox Studio, there's a method called FindFirstChild()
that can be used to see if something is in a given place, for example:
1 | workspace:FindFirstChild( "BasePlate" ) |
This would return true as there is a child in the workspace called BasePlate.
For your script, all you'd have to do is to add an if statement that checks if the model is in the workspace:
01 | local Plr = game.Players.LocalPlayer |
02 | local model = workspace.CommonBacon |
03 | local amountofcoins = 10 |
04 |
05 | Plr:GetMouse().KeyDown:Connect( function (Key) |
06 | if Key = = "e" then |
07 | wait ( 0.5 ) |
08 | if workspace:FindFirstChild( "CommonBacon" ) then |
09 | model:Destroy() |
10 | Plr.leaderstats.Coins.Value = Plr.leaderstats.Coins.Value + 10 |
11 | else |
12 | print ( "Model has already been destroyed" ) |
13 | end |
14 | end |
15 | end ) |