Script runcontext is client but the script runs on all clients

I have 2 scripts, one is for destroying a part locally, the other is to increment a value on the server side.

Script using local run context:

local players = game:GetService("Players")
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local plr = players:GetPlayerFromCharacter(hit.Parent)
		script.Disabled = true
		game.ReplicatedStorage.Events.ClockDestroy:FireServer(1)
		script.Parent.Noise:Play()
		task.wait(1)
		script.Parent.Parent:Destroy()
	end
end)

Script using server run context:

game.ReplicatedStorage.Events.ClockDestroy.OnServerEvent:Connect(function(plr, Number)
	if Number == 1 then
		plr:WaitForChild("PlrServerData").DualClocks.Value += 1
	end
end)

The issue: When a player collects one of the clocks, it removes it from all of the clients.

What i want to achieve: Make it so the player can get a clock, remove it on the client side, and increment their clock value by 1.

Just use LocalScript for that

the scripts are inside a part called hitbox

Well in such case you should check:


local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local plr = players:GetPlayerFromCharacter(hit.Parent)
if plr==nil or plr~=LocalPlayer  then return end
		script.Disabled = true
		game.ReplicatedStorage.Events.ClockDestroy:FireServer(1)
		script.Parent.Noise:Play()
		task.wait(1)
		script.Parent.Parent:Destroy()
	end
end)
i



Why?

Event could run on other clients aswell

1 Like

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