Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

1 comment

0

You never used amountofcoins, use an if statement, if model then on line 8

1 answer

✓ Accepted answer

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 comment

1

Thankyou so Much! im new to scripting or coding or whatever its called, but thankyou very much I just couldnt figure it out.

This discussion is preserved as it was. New answers, comments, and votes are closed.