script.Parent.Touched:Connect(function() seems to not work

I’m trying to do a client side sound effect and animation pop up whenever I touch / collect a coin. However though, the script seems to never run because every time I touch the coin it just doesn’t register. I’m not sure if I have to put this local script in starterplayerscripts or I’m doing this completely wrong. But everytime I collect a coin the animation nor sound plays. I’ve tried making it into a function like so – Local Script by the way inside of the part in workspace.

local function onTouch()
	print("Started")
	local coinsound = game.ReplicatedStorage.Coin
	local sound = coinsound:Clone()
	sound.Parent = workspace
	sound:Play()
	print("Sound Play")
	local r = Random.new()
	local Time = 0.25
	local ts = game:GetService('TweenService')
	local clonedGui = game.ReplicatedStorage.PopUpAnimations.Coin:Clone()
	clonedGui.Parent = game.Players.LocalPlayer.PlayerGui.Main
	clonedGui.Rotation = math.random(-50, 50)
	clonedGui.Position = UDim2.new(r:NextNumber(0,1), 0, 1, 0)
	clonedGui:TweenPosition(UDim2.new(r:NextNumber(0, 1), 0, r:NextNumber(0, 0.6), 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, Time)
	local rotationTween = ts:Create(clonedGui, TweenInfo.new(1), {Rotation = math.random(-90, 90)})
	rotationTween:Play()
	wait(1)
	local tween = ts:Create(clonedGui, TweenInfo.new(1), {ImageTransparency = 1})
	tween:Play()
	wait(3)
	clonedGui:Destroy()
	wait(3)
	sound:Destroy()
end

script.Parent.Touched:Connect(onTouch)

Then another solution I tried was – This time it was in StarterPlayerScripts

local function onTouch()
	print("Started")
	local coinsound = game.ReplicatedStorage.Coin
	local sound = coinsound:Clone()
	sound.Parent = workspace
	sound:Play()
	print("Sound Play")
	local r = Random.new()
	local Time = 0.25
	local ts = game:GetService('TweenService')
	local clonedGui = game.ReplicatedStorage.PopUpAnimations.Coin:Clone()
	clonedGui.Parent = game.Players.LocalPlayer.PlayerGui.Main
	clonedGui.Rotation = math.random(-50, 50)
	clonedGui.Position = UDim2.new(r:NextNumber(0,1), 0, 1, 0)
	clonedGui:TweenPosition(UDim2.new(r:NextNumber(0, 1), 0, r:NextNumber(0, 0.6), 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, Time)
	local rotationTween = ts:Create(clonedGui, TweenInfo.new(1), {Rotation = math.random(-90, 90)})
	rotationTween:Play()
	wait(1)
	local tween = ts:Create(clonedGui, TweenInfo.new(1), {ImageTransparency = 1})
	tween:Play()
	wait(3)
	clonedGui:Destroy()
	wait(3)
	sound:Destroy()
end

repeat wait() until workspace["Map1 Coins"] == nil
if workspace["Map1 Coins"] == nil then
	local map1 = workspace["Map1 Coins"]
	
	local coins = map1:GetChildren()
	
	coins.Touched:Connect(onTouch)
	
end

but the repeat wait until workspace[“Map1 Coins”] didn’t work at all.

1 Like

What if you use a server script in the part

i’ll try that and I’ll update you on the outcome with script

Thanks man, it worked.

WaitTime = 5
Amount = 1
function onTouched(part)
	local h = part.Parent:FindFirstChild("Humanoid")
	if (h~=nil) then
		local thisplr = game.Players:FindFirstChild(h.Parent.Name)
		if (thisplr~=nil) then
			local stats = thisplr:FindFirstChild("leaderstats")
			if (stats~=nil) then
				local coins = stats:FindFirstChild("Coins")
				local playergui = thisplr:FindFirstChild("PlayerGui")
				if (coins~=nil) then
					coins.Value = coins.Value + Amount
					if (playergui~=nil) then
						local r = Random.new()
						local Time = 0.25
						local ts = game:GetService('TweenService')
						local clonedGui = game.ReplicatedStorage.PopUpAnimations.Coin:Clone()
						clonedGui.Parent = playergui.Main
						clonedGui.Rotation = math.random(-50, 50)
						clonedGui.Position = UDim2.new(r:NextNumber(0,1), 0, 1, 0)
						clonedGui:TweenPosition(UDim2.new(r:NextNumber(0, 1), 0, r:NextNumber(0, 0.6), 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, Time)
						local rotationTween = ts:Create(clonedGui, TweenInfo.new(1), {Rotation = math.random(-90, 90)})
						rotationTween:Play()
						wait(1)
						local tween = ts:Create(clonedGui, TweenInfo.new(1), {ImageTransparency = 1})
						tween:Play()
						wait(3)
						clonedGui:Destroy()
					end
				end
			end
		end
		script.Parent.Transparency = 1
		script.Parent.icon.Transparency = 1
		script.Parent.icon2.Transparency = 1
		script.Disabled = true
		wait(WaitTime)
		script.Parent.Transparency = 0
		script.Parent.icon.Transparency = 0
		script.Parent.icon2.Transparency = 0
		script.Disabled = false
	end
end

script.Parent.Touched:Connect(onTouched)

No problem! Glad you got it fixed. Make sure you mark as solution