Touch script not working properly

I wanted to create a block that makes a sound and particle effects when you step on it.

Of course it does not work properly, I mean, it kind of does, but not always, one time when I turn the game on, it works perfectly, but another time, it glitches and the function which makes the script not repeat until the player moves away from the block does not work, resulting in a stack.

Here’s the info from the development console:

Error -- SoundScript is not a valid member of Part "Workspace.CeilingPanel"
Information -- Stack Begin
Information -- Script 'Workspace.CeilingPanel.RandomChance', Line 9 - function onTouched
Information -- Stack End

I tried a lot of different ways of fixing this issue, even entirely changing the script multiple times, but nothing worked, I really need help with fixing this issue.

Here’s the script itself:

script.Parent.Touched:Connect(function(h)
	if h and h.Parent:FindFirstChild("Humanoid") then
		script.Parent.CanTouch = false
		script.Parent.Step:Play()
		script.Parent.Smoke.Enabled = true
		script.Parent.Particles.Enabled = true
		wait(0.1)
		script.Parent.Smoke.Enabled = false
		script.Parent.Particles.Enabled = false


		repeat wait() until game.Players:GetPlayerFromCharacter(h.Parent):DistanceFromCharacter(script.Parent.Position) > 8
		script.Parent.CanTouch = true
	end
end)

I am thankful for all the help!

The error isn’t caused by the given script, since there is no referencing to SoundScript either that or u aren’t showing the correct code snippet.

2 Likes

There is nothing else that could be causing the SoundScript error, I am not sure where it’s from but it’s really the only script in the game so far.

this probably isnt the right script to show. But in the script thats shooting this error you can use a waitForChild maybe that’ll fix it

1 Like

Where would I place the waitForChild exactly?

when locating the instance. maybe something like

local script = workspace.CeilingPanel:WaitForChild("SoundScript")
1 Like
Warning -- Infinite yield possible on 'workspace.CeilingPanel:WaitForChild("SoundScript")'
Info -- Stack begin
Info -- Script 'Workspace.CeilingPanel.EffectScript', Line 1
info -- Stack End

That is what I get after implementing it, the script does not work anymore

1 Like

this means that maybe the item just isnt loading into game. Can you show me the script youre getting this error from?

1 Like
local script = workspace.CeilingPanel:WaitForChild("SoundScript")


script.Parent.Touched:Connect(function(h)
	if h and h.Parent:FindFirstChild("Humanoid") then
		script.Parent.CanTouch = false
		script.Parent.Step:Play()
		script.Parent.Smoke.Enabled = true
		script.Parent.Particles.Enabled = true
		wait(0.1)
		script.Parent.Smoke.Enabled = false
		script.Parent.Particles.Enabled = false


		repeat wait() until game.Players:GetPlayerFromCharacter(h.Parent):DistanceFromCharacter(script.Parent.Position) > 8
		script.Parent.CanTouch = true
	end
end)

Here it is, not sure if I added the WaitForChild in the correct place but it stops the script.

could you show me the view in explorer? maybe the script isnt inside the ceiling panel instance

1 Like

wgh

Here it is

1 Like

The issue is you have

local script = workspace.CeilingPanel:WaitForChild("SoundScript")

instead of

 local script = workspace.CeilingPanel:WaitForChild("EffectScript")

Theres no “SoundScript” inside your ceiling fan :sweat_smile:

1 Like

I did add it, tested it multiple times, appears to be working, I will get other scripts enabled and see if they may cause any issue.

1 Like

Okay so, the other script known as RandomChance might be now causing the issue, it’s supposed to roll a random number and if it’s a 1 it would disable the EffectScript for a short amount of time, there’s a chance these two might sync in such a way that it causes the error, I did think of an other idea tho, I will make it into one script where if it rolls any number between 2 and 99, the normal thing happens, but if it rolls 1, the other thing will happen! I will do it now and test it.

1 Like