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 4 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

local Plr = game.Players.LocalPlayer
local model = workspace.CommonBacon 
local amountofcoins = 10 

Plr:GetMouse().KeyDown:Connect(function(Key)
    if Key == "e" then
    wait (0.5)
        model:Destroy()
        Plr.leaderstats.Coins.Value = Plr.leaderstats.Coins.Value +10
    end
end)
0
You never used amountofcoins, use an if statement, if model then on line 8 greatneil80 2647 — 4y

1 answer

Log in to vote
1
Answered by
sydre 229 Moderation Voter
4 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:

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:

local Plr = game.Players.LocalPlayer
local model = workspace.CommonBacon 
local amountofcoins = 10 

Plr:GetMouse().KeyDown:Connect(function(Key)
    if Key == "e" then
    wait (0.5)
    if workspace:FindFirstChild("CommonBacon") then
            model:Destroy()
            Plr.leaderstats.Coins.Value = Plr.leaderstats.Coins.Value +10
        else
        print("Model has already been destroyed")
    end
    end
end)
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 — 4y
Ad

Answer this question