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

Made a crate drop script, how do I get the crate reward to work?

So I only picked up scripting a couple days ago and I've been experimenting with a lot of different things. I've spent a good amount of time making a crate spawn system and trying to figure out how to make it so that if a player touches the crate, they would get 10 cash. However, every attempt I've made seems to fail. Below you can see what I've ended up with. The top portion is the crate spawning script and the bottom is the reward script.

while true do
	local crate = game.Workspace.Crate:Clone()
	crate.Parent = workspace
	crate.Name = "MoneyCrate"
	crate.Transparency = 0
	crate.CanCollide = true
	crate.Anchored = false
	local X = math.random(-256,256)
	local Y = 100
	local Z = math.random(-256,256)
	crate.Position = Vector3.new(X,Y,Z)
	wait(60)
end

local function receiveMoney(part)
	if part.Parent:FindFirstChild("Humanoid") ~= nil then
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		player.leaderstats.Money.Value = player.leaderstats.Money.Value + 10
		game.Workspace.MoneyCrate:Destroy()
	end
end

game.Workspace.MoneyCrate.Touched:connect(receiveMoney)

2 answers

✓ Accepted answer

Heres the problem: You used a while true do loop, and it never stops. So, Lua never sees the code that is past the loop, because it runs code once it sees it, like any other programming language. For example, this script wont print something.

while true do
wait(60)
print('Hi!')
end

print("This will never get printed.") --This will never get printed, because the way how code runs.

Another problem you may have in the future and I want to solve now so you wont have it, is this.

print(MyFavColor)
local MyFavColor = 'Navy Blue'

It reads to print MyFavColor, but it does not know what MyFavColor means until line 2. Once it reaches line 2 and learns what MyFavColor means, it is too late, and it already errored. So what you should do for your script is to put the while loop at the end.

local function receiveMoney(part)
    if part.Parent:FindFirstChild("Humanoid") ~= nil then
        local player = game.Players:GetPlayerFromCharacter(part.Parent)
        player.leaderstats.Money.Value = player.leaderstats.Money.Value + 10
        game.Workspace.MoneyCrate:Destroy()
    end
end

game.Workspace.MoneyCrate.Touched:connect(receiveMoney)

while true do
    local crate = game.Workspace.Crate:Clone()
    crate.Parent = workspace
    crate.Name = "MoneyCrate"
    crate.Transparency = 0
    crate.CanCollide = true
    crate.Anchored = false
    local X = math.random(-256,256)
    local Y = 100
    local Z = math.random(-256,256)
    crate.Position = Vector3.new(X,Y,Z)
    wait(60)
end

Accept my answer if this helps!

1 comment

0

Thanks a lot for the advice, solved my problem and will be useful to know in the future.

Reading the Object Browser: If you go to the Object Browser in studio any thing that had a purple box is a function you can call. Anything that has a lightning bolt is an event. Anything that is a blue box is just a variable you can change

If you go to part and scroll down to the Lightning bolts it shows Touched and says Instance otherPart in parenthesis, this means that when the event runs your function, it will pass that as the parameter

That means when it's touched it will pass a parameter to the function you want with the other part, so if I were to touch it it would pass workspace.blowup999["Right Arm"]

workspace.Crate.Touched:Connect(receiveMoney) --Any questions about how this works just ask

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