I want to enable a lighting effect allways a player touch a part, but for some reason, it enable for all players and not only the player that touched the part
Because the other people will also be touching it on their own client’s screen.
Touch events are replicated from the original user’s client too. Ignore touches from anyone except the local player or instance the given Part on your client.
Check if the person who touched the part is the client.
Also it would be convenient for this situation if you had a function factory Toggle that would make a function to set the value of Effect.Enabled to whatever value you passed to the factory upon the new function being called.
local Workspace = game:GetService("Workspace")
local Lighting = game:GetService("Lighting")
local Players = game:GetService("Players")
local Part = Workspace.Part
local Effect = Lighting.Effect
local Client = Players.LocalPlayer
local function Toggle(Value)
return function(Hit)
if Hit:IsDescendantOf(Client.Character) then
Effect.Enabled = Value
end
end
end
workspace.Part.Touched:Connect(Toggle(true))
workspace.Part.TouchEnded:Connect(Toggle(false))
local Players = game:GetService("Players")
local Lightning = game:GetService("Lightning")
local Effect = Lighting.Effect
local Debounce = true
local WaitTime = 0.5
workspace.Part.Touched:Connect(function(hit)
local Check = hit:IsDescendantOf(Players.LocalPlayer.Character)
if Debounce and Check then
Debounce = not Debounce
Effect.Enabled = not Effect.Enabled
task.wait(WaitTime)
Debounce = not Debounce
end
end)
@D0RYU
That don’t works, the problem isn’t the effect not appear, the problem is it appear for all and it needs to appear only for client, the script of @DrKittyWaffles cheks if the client is the same player that touched.
And he uses the variables of workspace and part only to have less chance to the script fails when game start ( it never happend to me, but some players say that this already happend)
no I explain how he uses those two variables wrong
the Workspace variable was unneeded
and they never even use the Part variable, as you can see the last two lines start with workspace.Part