I have this script that damages a non-player humanoid then makes a noise, however it only works in studio mode. The scripts are in bricks that are spawned as bullets from a tool, these bricks are non-collide-able. I have tried making the bullets collide-able but that only makes the script more broken.
function enemy(hit)
local h = hit.Parent
if h:FindFirstChild("Humanoid") and
game.Players:GetPlayerFromCharacter(hit.Parent) == nil then
h:FindFirstChild("Humanoid").Health = h:FindFirstChild("Humanoid").Health - 30
local sone = script.Parent.s1
local stwo = script.Parent.s2
local sound = math.random(1, 2)
if sound == 1 then
sone:Play()
else if sound == 2 then
stwo:Play()
end
end
script.Parent:Destroy()
end
end
script.Parent.Touched:connect(enemy)
This is the script, it just goes right through the humanoid like a normal non-collideable.
function enemy(hit)
local h = hit.Parent
if h:FindFirstChild("Humanoid") and
game.Players:GetPlayerFromCharacter(hit.Parent) == nil then
h:FindFirstChild("Humanoid").Health = h:FindFirstChild("Humanoid").Health - 10
script.Parent.Sound:Play()
script.Parent:Destroy()
end
end
script.Parent.Touched:connect(enemy)
This is a modified version of the script used for a different tool, it sometimes damages humanoids if you stand really far away, it does not make a noise on impact though. It also just goes through humanoids rather than deleting itself.
function enemy(hit)
local h = hit.Parent
local sound = script.Parent.Sound
if h ~= nil and h and h:FindFirstChild("Humanoid") and
game.Players:GetPlayerFromCharacter(hit.Parent) == nil then
h:FindFirstChild("Humanoid").Health = h:FindFirstChild("Humanoid").Health - 1
sound:Play()
script.Parent:Destroy()
end
end
script.Parent.Touched:connect(enemy)
This is an even old version of the modified script before I tried fixing, it's a bit messier and works just as well as the other version.
UPDATE: I've added a half second delay before the script destroys its parent, so now sound plays when the modified script does damage, however the script still only sometimes works when you stand far away from the hit humanoid. I have also tried replacing 'blahblah.health = blahblah.health - number' to 'blahblah:TakeDamage(number)' but to no avail.
Any ideas on what I could do in order to get this to work in a normal game instance? I have a theory that it isn't working because of delay lag from the server, but I don't know how to put this into a local script due to the changing of health values.

2 comments
You spelled Sound as SOund? Is it supposed to be like that?
That was just an error from re-writing the script to how it was before I edited it, since I didn't have the old script saved to copy-paste