Help getting two scripts to work together

  1. **What do you want to achieve?

I am new to LUA script and really appreciate being on this forum with all of you helping people like me. I have two scripts and both of them work. When a monster dies he drops a small pile of gold. When you step on the gold it is collected in data storage. I also have a script when you step on the gold it plays a sound. For some reason I can only get one script to work at a time. I need them both to play when the gold is touched.

  1. What is the issue? Include screenshots / videos if possible!

I can make the gold play the sound or collect the gold, but I cant get them both to work.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I will show both scripts below, I have spent over 10 hours trying to make them work together in the same script, I tried seperating them into two scripts.

This is the gold collection script that removes the coins after collected.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if hit.Parent.Humanoid.Health > 0  then
			player.leaderstats.Gold.Value = player.leaderstats.Gold.Value + script.Parent.Gold.Value
			script.Parent:Remove()
		end
	end
end)

This is the script to play the gold collection sound on touch.

local sound = script.Parent.pickup
script.Parent.Touched:Connect(function(otherpart)
	local humanoid = otherpart.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		sound:Play()
	end
end)

Any idea how to get these to work at the same time or even in the same script. I am learning LUA by reverse engineering existing codes and youtube tutorials. Merging two scripts together really gets me confused.

Thanks everyone for reading and helping me out. This is my first post and I have already learned so much.

Why don’t you just play the sound when the coin is touched?:

local sound = script.Parent.pickup -- Defining the sound in the same script
script.Parent.Touched:Connect(function(hit) -- "hit" is the BasePart that touched "script.Parent". This is a variable
	if hit.Parent:FindFirstChild("Humanoid") then -- Seeing if an object called "Humanoid" is a child of the parent the object touched
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Getting the player if the character is found
		if hit.Parent.Humanoid.Health > 0  then -- Checking if the player is dead
            sound:Play() -- Playing the sound
			player.leaderstats.Gold.Value = player.leaderstats.Gold.Value + script.Parent.Gold.Value -- Increaseing the gold of the player
			script.Parent:Destroy() -- Destroy() is the new function to destroy, Remove() was deprecated
		end
	end
end)

I tried putting the sound into the script and couldn’t get the sound to play. I tried the script you provided and it collects the gold but no sound is played. If I just add the sound script the gold is not picked up but the sound plays. I think maybe it has something to do with the gold being removed after its picked up.

Here is a screenshot of how the gold is setup with the sound inside.

snip1

Try destroying the gold after you play the sound, but make it invisible so it seems like you’ve destroyed it:

local sound = script.Parent.pickup -- Defining the sound in the same script

local touchConnection -- Making a connection so we can stop the part detecting touches

touchConnection = script.Parent.Touched:Connect(function(hit) -- "hit" is the BasePart that touched "script.Parent". This is a variable
	if hit.Parent:FindFirstChild("Humanoid") then -- Seeing if an object called "Humanoid" is a child of the parent the object touched
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Getting the player if the character is found
		if hit.Parent.Humanoid.Health > 0  then -- Checking if the player is dead
            script.Parent.Transparency = 1 -- Setting the transparency to invisible
            touchConnection:Disconnect() -- Disconnecting the touched event so it doesn't fire again

            sound:Play() -- Playing the sound

            wait(sound.TimeLength) -- TimeLength is how long the sound is. We're waiting this long

			player.leaderstats.Gold.Value = player.leaderstats.Gold.Value + script.Parent.Gold.Value -- Increaseing the gold of the player
			script.Parent:Destroy() -- Destroy() is the new function to destroy, Remove() was deprecated
		end
	end
end)
2 Likes

Thanks so much. This works perfect.