Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

I need to make my code only be able to give 10 coins to the player if the Model is there?

Asked by 5 years ago

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

01local Plr = game.Players.LocalPlayer
02local model = workspace.CommonBacon
03local amountofcoins = 10
04 
05Plr: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
11end)
0
You never used amountofcoins, use an if statement, if model then on line 8 greatneil80 2647 — 5y

1 answer

Log in to vote
1
Answered by
sydre 229 Moderation Voter
5 years ago

In Roblox Studio, there's a method called FindFirstChild() that can be used to see if something is in a given place, for example:

1workspace: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:

01local Plr = game.Players.LocalPlayer
02local model = workspace.CommonBacon
03local amountofcoins = 10
04 
05Plr: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
15end)
1
Thankyou so Much! im new to scripting or coding or whatever its called, but thankyou very much I just couldnt figure it out. kollin3456789 4 — 5y
Ad

Answer this question