Normal script with runcontext: Client runs twice

When ball touches square it need to give money (like plinko) but it script runs twice, how to fix that?

for _, part in collectionService:GetTagged("multiplier") do
	local partMulti = part.Name
	part.Hitbox.Touched:Connect(function(hit)
		
		local val = hit:GetAttribute("Value")
		hit:Destroy()
		local summ = (((val * partMulti) * .5) * multi.Value)
		events.BallRemote:FireServer("AddCoins", summ)
		print(`{val} * {partMulti} = {summ}`)
	end)
	wait(1)
end

You can add a debounce to prevent it from running more than once at the same time:

local touchDebounce = false
for _, part in collectionService:GetTagged("multiplier") do
	local partMulti = part.Name
	part.Hitbox.Touched:Connect(function(hit)
		if touchDebounce then return end
		touchDebounce = true

		local val = hit:GetAttribute("Value")
		hit:Destroy()
		local summ = (((val * partMulti) * .5) * multi.Value)
		events.BallRemote:FireServer("AddCoins", summ)
		print(`{val} * {partMulti} = {summ}`)

		task.wait(1)
		touchDebounce = false
	end)
	wait(1)
end

i just used this instead (it runs 2 times - first time like server script, and second time like local script). So if it detecs as local script so it wont run:

for _, part in collectionService:GetTagged("multiplier") do
	
	local partMulti = part.Name
	part.Hitbox.Touched:Connect(function(hit)
		if script.Parent.Parent.Parent.Name == "StarterGui" then return end -- this part
		local val = hit:GetAttribute("Value")
		hit:Destroy()
		local summ = (((val * partMulti) * .5) * multi.Value)
		events.BallRemote:FireServer("AddCoins", summ)
		print(`{val} * {partMulti} = {summ}`)
	end)
	wait(1)
end

Wouldn’t it just be better to change runcontext to Server instead of client?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.