How do i make a script happen only to the player who touches it

lets say this is a script you have in a game

local Part = script.Parent
local debounce = false

function DamagePlayer(part)
	if debounce then
		return
			
	end
	if part.Parent:IsA("Accessory") then
		return
	end
	
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid then
		debounce = true

		humanoid.Health = humanoid.Health - 10

	else

	end
end


Part.Touched:Connect(DamagePlayer)


what i’ve noticed is that the “debounce” will make it so only one player can touch it any no body else will be able to. I know a way to fix that is to remove the “debounce” but i want it so it only fires once but can fire once per person.

ALSO if you can, whats a way to make the script only happen to the player?
for example, lets say you wanted to make a humanoid who touches a part have a GUI on their left side. how would you make it so it only happens to the player who touches the script?

1 Like

It would be best if you used a local script to make that.

i tried using a local script, but whenever i would the code wouldn’t run. Any reason why?

you have to do the touch event when the player touches the part, you have to make the frame visible = true

uhhhhhhhhhhhhhhhhhhhhhhhh how would i do that?

Put this script in the part that damages the player:

script.Parent.Touched:Connect(function(touch)
if touch.Parent:FindFirstChild(“Humanoid”) then
touch.Parent.Humanoid.Health -= 10
end
end)

To make it fire once per person put a local script in the part and put this in the local script:
script.Parent.Touched:Connect(function(touch)
if touch.Parent:FindFirstChild(“Humanoid”) then
touch.Parent.Humanoid.Health -= 10
script.Parent.CanTouch = false
end
end)

oh okay but I have a question:

since my script and that script is basically the same then,

why wouldnt mine work in a local script?

I don’t really know. Also, I didn’t test if my script worked in a local script. Does mine work in a local script?

it doesn’t work in a local script as the .Touched event shouldn’t be used in a local script
instead detect it using the server script and fire a Remote Event Bindable Events and Functions | Roblox Creator Documentation

LocalScripts don’t run in the workspace. @robloxguy9093ALT

local setPlayer

script.Parent.Touched:Connect(function(h)
    local c = h.Parent.Name
    local player = game:GetService('Players'):FindFirstChild(c)
    if player then
       setPlayer = player
    end
end)

-- now the touchpart variable will be the player who touched it thus enabling you to do only whatever to that player through the variable.

It can be used in a local script. The local script just has to NOT be a descendant of the workspace.

1 Like

yes I know it can but it sholdn’t also yes it obv can’t be a descendant of workspace as it needs to load for each player

1 Like

Imma just do this my way:

  1. Add a local script (or server/normal script it doesnt matter much for this kind of script) in the StarterCharacterScript
  2. the code in the script should be like:

(if this is a local script and the player is R6 animation)
local Player = game.Players.LocalPlayer
local Torso = Player.Character.Torso
local once = false

Torso.Ontouched:connect(function(damage)
if once == false then
once = true
Player.Character.Humanoid.Health = Player.Character.Humanoid.Health - 1
end
end)

  1. done

Responding to the original post, I often like to make a dictionary with different instances in this case. That way, each character has its own debounce to be checked and set.

local Part = script.Parent
local debounces = {}

function DamagePlayer(part)
	if debounces[part.Parent] then
		return
			
	end
	if part.Parent:IsA("Accessory") then
		return
	end
	
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid then
		debounces[part.Parent] = true

		humanoid.Health = humanoid.Health - 10

	else

	end
end


Part.Touched:Connect(DamagePlayer)
2 Likes