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

Touching brick not working ?

local sound = script.BiomeA

function onTouched(Player)
	sound:Clone().Parent=Player
	Player.BiomeA:Play()	
end

script.Parent.Touched:connect(onTouched)

Once the player touches the brick it should clone sound to player, play it and never again, till he touches another brick that stops the sound.

And what it does is just plays on the brick when i touch it and spams the sound inside and i can't even find where sound clones not even in brick... problem ? ;-;

Try this on ur own studio paste this script in a brick and in the script, put a sound named BiomeA and a sound u want then watch what happens when u touch the brick the sound is weird spams alot and cannot be found anywhere... whats wrong >????

Ah i found that it pastes it into the players head hands torso etc... but i want to only do it once so it pastes into him locally and only he can hear it no one else

1 answer

✓ Accepted answer

Your script has a few problems.

The Touch Event doesn't return the player, it gives you the part that touched the object.

To get the player, let's use the GetPlayerFromCharacter method.

-- Regular Script
local sound = script.BiomeA

function onTouched(part)
	local plr = game.Players:GetPlayerFromCharacter(part.Parent)
	if plr then
		-- Do Stuff
	end
end

script.Parent.Touched:connect(onTouched)

Use a debounce.

A Debounce is used to stop code from playing multiple times in a row.

-- Regular Script
local sound = script:WaitForChild("BiomeA")
local Debounce = false

function onTouched(part)
	if Debounce then return end
	Debounce = true
	local plr = game.Players:GetPlayerFromCharacter(part.Parent)
	if plr then
		-- Do Stuff
	end
	wait(2)-- Modify
	Debounce = false
end

script.Parent.Touched:connect(onTouched)
Where it says modify, you can change how long until the code will run again.

Adding your code.

Now let's just throw your code in there,

-- Regular Script
local sound = script:WaitForChild("BiomeA")
local Debounce = false

function onTouched(part)
	if Debounce then return end
	Debounce = true
	local plr = game.Players:GetPlayerFromCharacter(part.Parent)
	if plr then
		local SoundCopy = sound:Clone()
		SoundCopy.Parent = plr
		SoundCopy:Play()
	end
	wait(2)
	Debounce = false
end

script.Parent.Touched:connect(onTouched)
I opted to use a variable for the sound clone just in case we want to access it later.

That should do it. Hope I helped.

Good Luck!

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