Need help fixing this error

I’m getting this error on line 55 and I’m not sure how to fix it, some help would be appreciated.

The Error: Players.King09838.PlayerScripts.RainScript:55: attempt to index nil with ‘Touched’

Script:

local Rain = require(script.Rain)
local SoundService = game:GetService("SoundService")
Rain:SetSoundId(script.SoundIdInside.Value)

Rain:SetColor(Color3.fromRGB(script.Color.Value.x, script.Color.Value.y, script.Color.Value.z))
Rain:SetDirection(script.Direction.Value)

Rain:SetTransparency(script.Transparency.Value)
Rain:SetSpeedRatio(script.SpeedRatio.Value)
Rain:SetIntensityRatio(script.IntensityRatio.Value)
Rain:SetLightInfluence(script.LightInfluence.Value)
Rain:SetLightEmission(script.LightEmission.Value)

Rain:SetVolume(script.Volume.Value)

Rain:SetSoundId(script.SoundId.Value)
Rain:SetStraightTexture(script.StraightTexture.Value)
Rain:SetTopDownTexture(script.TopDownTexture.Value)
Rain:SetSplashTexture(script.SplashTexture.Value)

local threshold = script.TransparencyThreshold.Value
if script.TransparencyConstraint.Value and script.CanCollideConstraint.Value then
	Rain:SetCollisionMode(
		Rain.CollisionMode.Function,
		function(p)
			return p.Transparency <= threshold and p.CanCollide
		end
	)
elseif script.TransparencyConstraint.Value then
	Rain:SetCollisionMode(
		Rain.CollisionMode.Function,
		function(p)
			return p.Transparency <= threshold
		end
	)
elseif script.CanCollideConstraint.Value then
	Rain:SetCollisionMode(
		Rain.CollisionMode.Function,
		function(p)
			return p.CanCollide
		end
	)
end

local SoundPart = game.Workspace:FindFirstChild("SoundPart")
local isTouching = false

local seconds = 900
local raintime = 515

while wait(seconds)do
	
	Rain:Enable()
	
	SoundPart.Touched:Connect(function(hit)
		if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
			if Rain:Enabled() then
				-- Player touched the SoundPart while the rain is enabled
				isTouching = true
				SoundService:PlayLocalSound(script.SoundIdInside.Value)
			end
		end
	end)
	
	wait(raintime)
	Rain:Disable()
	
	SoundPart.TouchEnded:Connect(function(hit)
		if hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
			if Rain:Disable() then
				-- Player touched the SoundPart while the rain is enabled
				isTouching = false
				SoundService:StopLocalSound(script.SoundIdInside.Value)
			end
		end
	end)
end
2 Likes

You mind pointing out where line 55 is??

It means the script is unable to find the variable SoundPart resulting it to be nil. Are you sure the reference to the object is correct and everything?

This is line 55 SoundPart.Touched:Connect(function(hit)

Yeah its correct but there used to be an error on this line local SoundPart = game.Workspace:FindFirstChild(“SoundPart”) that said SoundPart wasn’t a valid member of workspace when it literally was.

Use WaitForChild instead of FindFirstChild

nope I still got the same error

Im not too sure what the issue could be but it could be that the client cannot access the object for some reason perhaps streaming enabled is on?

I’ve always gotten errors when using the and statements, so I suggest just using a separate if then if then statement. Basically replacing this

SoundPart.TouchEnded:Connect(function(hit)
	if hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
		if Rain:Disable() then
			-- Player touched the SoundPart while the rain is enabled
			isTouching = false
			SoundService:StopLocalSound(script.SoundIdInside.Value)
		end
	end
end)

into this

SoundPart.TouchEnded:Connect(function(hit)
	if hit.Parent then 
		if hit.Parent:FindFirstChildOfClass("Humanoid") then
			if Rain:Disable() then
				-- Player touched the SoundPart while the rain is enabled
				isTouching = false
				SoundService:StopLocalSound(script.SoundIdInside.Value)
			end
		end
	end
end)
1 Like

still got the same error

local Rain = require(script.Rain)
local SoundService = game:GetService("SoundService")
Rain:SetSoundId(script.SoundIdInside.Value)

Rain:SetColor(Color3.fromRGB(script.Color.Value.x, script.Color.Value.y, script.Color.Value.z))
Rain:SetDirection(script.Direction.Value)

Rain:SetTransparency(script.Transparency.Value)
Rain:SetSpeedRatio(script.SpeedRatio.Value)
Rain:SetIntensityRatio(script.IntensityRatio.Value)
Rain:SetLightInfluence(script.LightInfluence.Value)
Rain:SetLightEmission(script.LightEmission.Value)

Rain:SetVolume(script.Volume.Value)

Rain:SetSoundId(script.SoundId.Value)
Rain:SetStraightTexture(script.StraightTexture.Value)
Rain:SetTopDownTexture(script.TopDownTexture.Value)
Rain:SetSplashTexture(script.SplashTexture.Value)

local threshold = script.TransparencyThreshold.Value
if script.TransparencyConstraint.Value and script.CanCollideConstraint.Value then
	Rain:SetCollisionMode(
		Rain.CollisionMode.Function,
		function(p)
			return p.Transparency <= threshold and p.CanCollide
		end
	)
elseif script.TransparencyConstraint.Value then
	Rain:SetCollisionMode(
		Rain.CollisionMode.Function,
		function(p)
			return p.Transparency <= threshold
		end
	)
elseif script.CanCollideConstraint.Value then
	Rain:SetCollisionMode(
		Rain.CollisionMode.Function,
		function(p)
			return p.CanCollide
		end
	)
end

local SoundPart = game.Workspace:FindFirstChild("SoundPart")
local isTouching = false

local seconds = 20
local raintime = 515

while wait(seconds)do
	
	Rain:Enable()
	
	SoundPart.Touched:Connect(function(hit)
		if hit.Parent then
			if hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
			   if Rain:Enabled() then
				-- Player touched the SoundPart while the rain is enabled
				isTouching = true
				SoundService:PlayLocalSound(script.SoundIdInside.Value)
			end
		end
	end	end)
end
	
	wait(raintime)
	Rain:Disable()
	
	SoundPart.TouchEnded:Connect(function(hit)
		if hit.Parent then 
		    if hit.Parent:FindFirstChildOfClass("Humanoid") then
				if Rain:Disable() then
					-- Player touched the SoundPart while the rain is enabled
					isTouching = false
					SoundService:StopLocalSound(script.SoundIdInside.Value)
				end
			end
		end
	end)