Hi I'm trying to make a grenade tool and I'm working on the damage this is what I got
hitplayers = {} script.Parent.Touched:Connect(function(hit) local obj = hit.Parent local hum = hit.Parent:FindFirstChild("Humanoid") if hit.Parent.Name == "Outrider" and hit.Parent ~= script.Wielder.Value and hit.Parent ~= hitplayers then table.insert(hitplayers,hit.Parent) hum:TakeDamage(50) end end)
I'm trying to make it so it wont hit the same player twice how do I fix this?
You're attempting to compare an object with an array! You would have to iterate through hitplayers
and compare each index.. but rather than inserting the object as the value, insert it as the key for ease of access! I'll show you:
local hitplayers = {}; script.Parent.Touched:Connect(function(hit) local obj = hit.Parent local hum = hit.Parent:FindFirstChild("Humanoid") --separated conditions so it looks cleaner local outrider = hit.Parent.Name == "Outrider"; local noWield = hit.Parent ~= script.Wielder.Value; local alreadyHit = hitplayers[obj]; --ease of access if outrider and noWield and not alreadyHit then hitplayers[obj] = true; --set as key hum:TakeDamage(50) end end)
local DamagedPlayers = {} function checkDamagedPlayers(PlayerName) for i,v in pairs(DamagedPlayers) do if v == PlayerName then return true end end return false end function onTouched(Part) local Character = Part:FindFirstAncestorWhichIsA("Model") if Character and Character.Humanoid then local inDamagedPlayers = checkDamagedPlayers(Character.Name) if Character.Name == "Outrider" and Character ~= script.Wielder.Value and inDamagedPlayers ~= true then table.insert(DamagedPlayers,Character) Character.Humanoid:TakeDamage(50) end end end script.Parent.Touched(onTouched)
We have declare a table called "DamagedPlayers", and a function that checks if a specific item is within the table. The damage code is the same except we use the function checkDamagedPlayers.