So I made this simple script that works almost perfectly. It's basically a part and when you step on it a TextLabel will appear on your screen, simple, but the debounce part ruins it. If 2 people step on the part at the same time the text will only show for 1 person, and nothing for the other. This is because of the debounce part. You could fix this by removing the debounce, but that is not very efficient, because the debounce is an important part of the script. I would really appreciate a good answer, I really need this to work.
Popup = script.Parent.Popup debounce = true function onTouch(hit) local all = hit.Parent:FindFirstChild("Humanoid") if all ~= nil and debounce == true then debounce = false local player = game.Players:FindFirstChild(all.Parent.Name) local Gui = Popup:clone() Gui.Parent = player.PlayerGui wait(4) Gui:remove() wait(1) debounce = true end end script.Parent.Touched:connect(onTouch)
Your best option is to move the code to a LocalScript so that it runs on the client, and can be coded just as you have done. The caveat here is that you can't just put a LocalScript into a Model or part in Workspace, the script has to reside in a place where LocalScripts can execute from or get clone to an executable place from (LocalScripts in StarterPlayerScripts, StarterGui, etc. get copied into each players' PlayerScripts or PlayerGui respectively), and it needs to get a reference to the part or model it's associate with via a WaitForChild() call.
To make your script work as a server Script, it needs to be multiplayer-aware, which means you can't have a single debounce boolean, you need one bool per player. Normally, this sort of thing would be done with a table, like so (note: untested edits to your code):
local Popup = script.Parent.Popup local debounce = {} function onTouch(hit) local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid") local player = humanoid and game.Players:FindFirstChild(humanoid.Parent.Name) if player and (not debounce[player]) then debounce[player] = true local Gui = Popup:clone() Gui.Parent = player.PlayerGui Wait(4) Gui:Destroy() Wait(1) debounce[player] = nil end end script.Parent.Touched:connect(onTouch)
That said, the code should properly be moved to a LocalScript. Doing this sort of thing on the server means everyone sees the popup's appearance lag by the amount of their round-trip ping time + time to replicate the necessary Gui. Even when you have a touched event that changes server state, it can be beneficial to process it on the client and then send a RemoteEvent to the server if the server needs to also do something.
For first, :connect
, :remove
and :clone
its deprecated, use: :Connect
, :Destroy
and :Clone
, And i recommend to use local variables, not variable = ...
, but use local variable = ...
This happens because the debounce its for one player only, for make a debounce for every player you need a table to store character, player name or UserId. not a variable
Example:
local Debounces = {} local function onTouch(Hit) if Hit.Parent:FindFirstChildOfClass("Humanoid") then --> Try to find a humanoid... local Player = game.Players:GetPlayerFromCharacter(Hit.Parent) --> Try to get the player if Player and not Debounces[Player.UserId] then --> This gets the player and check if Current Player Debounce is false or nil Debounces[Player.UserId] = true --> Set Current Player debounce to true print("Debounce") wait(4) --> After 4 seconds Debounces[Player.UserId] = false --> Set Current Player debounce to false end end end script.Parent.Touched:Connect(onTouch)
With that, every player that touches, has a local debounce...
For your script only need to adjust that..
local Popup = script.Parent.Popup local Debounces = {} local function onTouch(Hit) if Hit.Parent:FindFirstChildOfClass("Humanoid") then local Player = game.Players:GetPlayerFromCharacter(Hit.Parent) if Player and not Debounces[Player.UserId] then Debounces[Player.UserId] = true local Gui = Popup:Clone() Gui.Parent = Player:WaitForChild("PlayerGui") wait(4) Gui:Destroy() wait(1) Debounces[Player.UserId] = false end end end script.Parent.Touched:Connect(onTouch)
Hope it helped! :)