local namePart = workspace.NamePart local nameText = namePart.SurfaceGui.TextLabel script.Parent.Touched:Connect(function(hit) local hum = hit.Parent:FindFirstChild("Humanoid") local p = game.Players:GetPlayerFromCharacter(hit.Parent) local PlayerName = p.Name if hum and p then if p.UserId == game.CreatorId then script.Parent.CanCollide = false script.Parent.Transparency = .25 wait(3) script.Parent.CanCollide = true script.Parent.Transparency = 0.5 else hit.Parent:BreakJoints() end if hum and p and namePart and nameText then nameText.Text = p.Name end end end) script.Parent.Touched:Connect(function(hit) local h = hit.Parent:FindFirstChild("Humanoid") local p = game.Players:GetPlayerFromCharacter(hit.Parent) local PlayerName = p.Name if nameText and h and p then if nameText.Text ~= PlayerName then print("the name is different") h.Health = 0 end end end)
this is a server script parented to a part in the workspace everything works fine but i want that only the player that touched it first to be able to walk through it any help is apriciated
You should not make two connections to the same Touched event, this just complicates things unnecessarily. You've created a race condition, where the second Touched event handler is checking nameText.Text, expecting it to have been set by the other handler function, except that the first one has a Wait(3)
in it, so nameText.Text is getting set about 3 seconds after you've already killed your dude with h.health = 0.
Refactor this into a single event handler function, and go from there.
You also are not doing nil checks in enough places, or early enough. This code:
local p = game.Players:GetPlayerFromCharacter(hit.Parent) local PlayerName = p.Name
can easily spam errors, because hit.Parent is not necessarily a character Model. If hit
is the Handle part of an Accessory, for example, hit.Parent will be an Accessory and p will be nil.
Debounce is like a cooldown for an event. (e.g "You have gun and you want cooldown on it. So you use debounce to do it")
--Cooldown Debounce local debounce = false object.Event:Connect(function()) if debounce then return end debounce = true --CODE wait(cooldown) debounce = false end) --Forerver debounce local debounce = false part.Touched:connect(function(hit)) if debounce then return end --CODE end)
You want to use the second one. put the code inside of the event in your event.