Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

my owner only door script if anyone touches it it updates the name on the board but kills him. why?

Asked by
seikkatsu 110
4 years ago
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

0
Does it give you any errors? I looked at it, and it seems fine. I also highly doubt you wrote it yourself. Sonnenroboter 336 — 4y
0
this is why you use a debounce DeceptiveCaster 3761 — 4y
0
can i get an example of that please? seikkatsu 110 — 4y
0
and i actually got some help from a yt video and the rest is my own work seikkatsu 110 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

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.

Ad
Log in to vote
0
Answered by 4 years ago

You need to use debounce

What is debounce

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")

Debounce code

--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.

0
This will not actually fix his problem, unfortunately. EmilyBendsSpace 1025 — 4y
0
Why not? Luka_Gaming07 534 — 4y
0
see emily's response royaltoe 5144 — 4y

Answer this question