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

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

Asked by 6 years ago

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

Log in to vote
1
Answered by 6 years ago
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)
0
Change the variable NPC to whatever the NPC model is. DeathGunner132 120 — 6y
0
```lua Delude_d 112 — 6y
Ad
Log in to vote
0
Answered by
amanda 1059 Moderation Voter
6 years ago
Edited 6 years ago

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)
0
Amanda, your one doesn't quite work.. Delude_d 112 — 6y
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. amanda 1059 — 6y
Log in to vote
0
Answered by
Delude_d 112
6 years ago

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)

Answer this question