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

Does anyone know how to make a script only run if touched by a player?

I want to make a game where players run from a NPC which I am adding power-ups to. The only problem is that when a NPC touches it makes the NPC go faster when I want it to only make the player go faster

local hitBox = script.Parent.HitBox
local debounce = true

hitBox.Touched:Connect(function(hit)
	if debounce == true then 
		debounce = false
		local char = hit.Parent
		if char:FindFirstChild("Humanoid") then
			hitBox.Transparency = 1
			hitBox.Parent.InnerCore.Transparency =1
			char.Humanoid.WalkSpeed = 40
			wait(2)
			char.Humanoid.WalkSpeed = 30
			hitBox.Parent:Destroy()
		end
	end
end)

3 answers

local hitBox = script.Parent.HitBox
local debounce = true

local NPC = script.Parent -- Assuming script.Parent is the NPC you're talking about

hitBox.Touched:Connect(function(hit)
    if debounce == true then 
        debounce = false
        local char = hit.Parent
	if char ~= NPC then -- Solution
	        if char:FindFirstChild("Humanoid") then
	            hitBox.Transparency = 1
	            hitBox.Parent.InnerCore.Transparency =1
	            char.Humanoid.WalkSpeed = 40
	            wait(2)
	            char.Humanoid.WalkSpeed = 30
	            hitBox.Parent:Destroy()
	        end
	end
    end
end)

2 comments

0

Change the variable NPC to whatever the NPC model is.

0

You should use the GetPlayerFromCharacter method from the Players service to verify the Character Model touching the part is linked to a Player.

local players = game:GetService("Players")
local hitBox = script.Parent.HitBox
local debounce = true

hitBox.Touched:Connect(function(hit)
    if debounce == true then 
        debounce = false
        local char = hit.Parent
        if players:GetPlayerFromCharacter(char) then
            hitBox.Transparency = 1
            hitBox.Parent.InnerCore.Transparency = 1
            char.Humanoid.WalkSpeed = 40
            wait(2)
            char.Humanoid.WalkSpeed = 30
            hitBox.Parent:Destroy()
        end
    end
end)

2 comments

0

Amanda, your one doesn't quite work..

0

My bad, I misspelled my variable name at the top, should be "players" instead of "player". I have just edited it, please try again.

TESTED THIS AND WORKS

local hitBox = script.Parent.HitBox
local debounce = true

NPCs = {
       "Zombie";
       "Fighter"
}


hitBox.Touched:Connect(function(hit)
 local char = hit.Parent
for _,v in pairs(NPCs) do
    if debounce == false and char.Name == v then return end
        debounce = false
        if char:FindFirstChild("Humanoid") then
            hitBox.Transparency = 1
            hitBox.Parent.InnerCore.Transparency =1
            char.Humanoid.WalkSpeed = 40
print("wow")
            wait(2)
            char.Humanoid.WalkSpeed = 30
            hitBox.Parent:Destroy()
    end
end
end)

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