Sound on collision issue

Hey there! I’m relatively new to scripting in Roblox studio so you’ll most likely see me a bit on here. I always seem to run into some sort of issue, but always find a solution on here. So far, I have not found one regarding my current issue so I felt like I should finally jump into some posts. My wife, my 7 and 6 year old and myself have started a roblox game creation journey and can use all the tips we can get.

My issue has to do with collision and sounds playing. I know it sounds super simple, and might very well be. However, I can’t seem to wrap my head around it. Perhaps I’m doing it all wrong. But I’ll try to give as much information as possible as well as what I’ve tried.

What I’m trying to achieve is that when a player collides with a model(ball), it plays a sound and disappears. It’s for one of those tycoon-esque games that people seem to like playing but the dev community hates. My kids are in love with them so I thought it would be a fun thing to make as a family. As well as teach them coding in the process. (Sorry in advance.)

It actually worked at first, but when the player collided with the ball, the ball still kept the shape after it disappeared and would cause the player to stop on collision(The sound on collision worked here). So I read into collision groups and after seeing some of the issues answered on here I was able to part together a solution for that issue. The collision group method seemed to work better than the script itself. However I’m at a point where I think they might be conflicting with each other.

Here is an image of the “Dropper” hierarchy itself.

dropper

Here is the CollectScript which has the collision issue. It was meant to play a sound when the ball was touched and disappeared. ( I’m using this script to make the GoldBall disappear. )

local db = true
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if db == true then
			db = false
			script.Parent.Transparency = 1
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
			script.Sound:Play()
			wait(1)
			script.Parent:Remove()
		end
	end	
end)

As for the collisiongroup script, I just have it in the script service like so.

scriptstorage

local PS = game.PhysicsService
local Goldball = game.ServerStorage.Balls.GoldBall
local Quartzball = game.ServerStorage.Balls.QuartzBall
local Silverball = game.ServerStorage.Balls.SilverBall

PS:CreateCollisionGroup("Ball")
PS:CreateCollisionGroup("Players")
PS:CollisionGroupSetCollidable("Ball","Players",false)
PS:SetPartCollisionGroup(Goldball,"Ball")
PS:SetPartCollisionGroup(Quartzball,"Ball")
PS:SetPartCollisionGroup(Silverball,"Ball")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		
		player.CharacterAppearanceLoaded:Wait()

		for _, p in pairs(player.Character:GetChildren()) do
			if p:IsA("Part") or p:IsA("MeshPart") then
				PS:SetPartCollisionGroup(p,"Players")
			end
		end
	end)	
end)	

My sounds are just a small popping noise, like most games. I was wondering if maybe there was a time length minimum limit it would have to be.

I’ve tried messing with in game volume, the properties of the sound files itself as well as trying to use the sound service which I can’t seem to do correctly.

Any help and tips would be appreciated! Sorry for the dreadfully long post. I just feel the more details the better.

Also, by chance, if anyone can point me to a topic or documentation on how to merge a dropper, that would be the bees knees.

Thanks for reading!

After a first read through i would say you need to finish the debounce by returning it to true, if you want it to work again on the next touched event:

local db = true
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if db == true then
			db = false
			script.Parent.Transparency = 1
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
			script.Sound:Play()
			wait(1)
			script.Parent:Remove()
            db = true
		end
	end	
end)

This video has a good explanation on how you could better handle the touched event

You could do something similar to that video then connect a remote or use local script with touched event for the sound. I just parent the sound to the player’s head, or the entity they are interacting with

function playSound(part, id)
	if part:FindFirstChild("Sound") then
		return
	end
	local sound = Instance.new("Sound")
	sound.Parent = part
	sound.Volume = 0.16
	sound.RollOffMaxDistance = 200
	sound.SoundId = "rbxassetid://" .. tostring(id)
	sound:Play()
	delay(sound.TimeLength, function()
		sound:Destroy()
	end)
end

Also you can put any sounds you use into ReplicatedStorage or ReplicatedFirst so they are loaded prior to the first play

The part is being deleted as well as the script. Setting it back to true would not make it work again.
As for the collision issue, setting the transparency does not influence the collision. However, you can just delete everything right there instead of waiting a second.

local db = true
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if db == true then
			db = false
			script.Parent:Destroy() -- do this instead of Remove() since it handles disconnections as well
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
		end
	end	
end)

Set this property to true for the sound object, you dont have to manually play it for this case.
image

1 Like

This seems to have done it! I’m not sure why I went the transparency route. Deleting does make much more sense. Also as for the PlayOnRemove, I hade a feeling that it did as described and ticked that as well but that didn’t seem to work.

Thanks a ton! Now onto merging and all that jazz.

1 Like