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
01 | local hitBox = script.Parent.HitBox |
02 | local debounce = true |
03 |
04 | hitBox.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 |
17 | end ) |
01 | local hitBox = script.Parent.HitBox |
02 | local debounce = true |
03 |
04 | local NPC = script.Parent -- Assuming script.Parent is the NPC you're talking about |
05 |
06 | hitBox.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 ) |
You should use the GetPlayerFromCharacter
method from the Players
service to verify the Character Model
touching the part is linked to a Player
.
01 | local players = game:GetService( "Players" ) |
02 | local hitBox = script.Parent.HitBox |
03 | local debounce = true |
04 |
05 | hitBox.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 |
18 | end ) |
TESTED THIS AND WORKS
01 | local hitBox = script.Parent.HitBox |
02 | local debounce = true |
03 |
04 | NPCs = { |
05 | "Zombie" ; |
06 | "Fighter" |
07 | } |
08 |
09 |
10 | hitBox.Touched:Connect( function (hit) |
11 | local char = hit.Parent |
12 | for _,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 |