Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

4 comments

0

Does it give you any errors? I looked at it, and it seems fine. I also highly doubt you wrote it yourself.

0

this is why you use a debounce

0

can i get an example of that please?

0

and i actually got some help from a yt video and the rest is my own work

2 answers

✓ Accepted answer

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.

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

3 comments

0

This will not actually fix his problem, unfortunately.

0

Why not?

0

see emily's response

This discussion is preserved as it was. New answers, comments, and votes are closed.