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

01local hitBox = script.Parent.HitBox
02local debounce = true
03 
04hitBox.Touched:Connect(function(hit)
05    if debounce == true then
06        debounce = false
07        local char = hit.Parent
08        if char:FindFirstChild("Humanoid") then
09            hitBox.Transparency = 1
10            hitBox.Parent.InnerCore.Transparency =1
11            char.Humanoid.WalkSpeed = 40
12            wait(2)
13            char.Humanoid.WalkSpeed = 30
14            hitBox.Parent:Destroy()
15        end
16    end
17end)

3 answers

Log in to vote
1
Answered by 6 years ago
01local hitBox = script.Parent.HitBox
02local debounce = true
03 
04local NPC = script.Parent -- Assuming script.Parent is the NPC you're talking about
05 
06hitBox.Touched:Connect(function(hit)
07    if debounce == true then
08        debounce = false
09        local char = hit.Parent
10    if char ~= NPC then -- Solution
11            if char:FindFirstChild("Humanoid") then
12                hitBox.Transparency = 1
13                hitBox.Parent.InnerCore.Transparency =1
14                char.Humanoid.WalkSpeed = 40
15                wait(2)
View all 21 lines...
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.

01local players = game:GetService("Players")
02local hitBox = script.Parent.HitBox
03local debounce = true
04 
05hitBox.Touched:Connect(function(hit)
06    if debounce == true then
07        debounce = false
08        local char = hit.Parent
09        if players:GetPlayerFromCharacter(char) then
10            hitBox.Transparency = 1
11            hitBox.Parent.InnerCore.Transparency = 1
12            char.Humanoid.WalkSpeed = 40
13            wait(2)
14            char.Humanoid.WalkSpeed = 30
15            hitBox.Parent:Destroy()
16        end
17    end
18end)
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

01local hitBox = script.Parent.HitBox
02local debounce = true
03 
04NPCs = {
05       "Zombie";
06       "Fighter"
07}
08 
09 
10hitBox.Touched:Connect(function(hit)
11 local char = hit.Parent
12for _,v in pairs(NPCs) do
13    if debounce == false and char.Name == v then return end
14        debounce = false
15        if char:FindFirstChild("Humanoid") then
View all 25 lines...

Answer this question