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)
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)
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)
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)