Just a couple hours ago, I’ve had this massive issue with my script that popped up. Scripts in my game that detect players death have about a 50/50 chance of actually detecting it. Half the time it wont connect, half the time it will. The 2 scripts are as basic as it gets, its a basic ragdoll script that you can get off of the toolbox and a sound on death script. The ragdoll script is probably too big to post so i’ll just do the sound one since they both don’t feel like working anyways.
function Died(p)
wait(.001)
local tracks=script:GetChildren()
local rn=math.random(1,#tracks)
local track=tracks[rn]
if track~=nil then
track:play()
end
end
function Hum(p)
hum = p:findFirstChild("Humanoid")
if hum ~= nil then
print("Player Died") -- NOT PRINTING
hum.Died:connect(function(hum) Died(p) end)
end
end
function Enter(p)
p.CharacterAdded:connect(Hum)
end
game.Players.PlayerAdded:connect(Enter)
If anyone can help me with this it will be greatly appreciated.
Try a re-arrangement. Maybe the character somehow already existed before player?
local Players = game:GetService("Players")
local Tracks = script:GetChildren() -- huh
local function onCharacterAdded(character)
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
print("Signal routed")
humanoid.Died:Connect(function()
local randomNumber = math.random(#Tracks)
local track = Tracks[randomNumber]
if track then
track:Play()
end
end) -- if it errored, this was missing before
end
end
local function onPlayerAdded(player)
if player.Character then
onCharacterAdded(player.Character)
end
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
Maybe it is because the player has joined, but their Character/Humanoid has yet to load in. Try giving it at least 1 second delay before connecting the function.
function Died(p)
print("Player Died") -- PRINT FIRST
wait(.001) -- NOTICE THERE IS A WAIT(.001)
local tracks=script:GetChildren()
local rn=math.random(1,#tracks)
local track=tracks[rn]
if track~=nil then
track:play()
end
end
function Hum(p)
hum = p:findFirstChild("Humanoid")
if hum ~= nil then
hum.Died:connect(function(hum) Died(p) end)
end
end
function Enter(p)
p.CharacterAdded:connect(Hum)
end
game.Players.PlayerAdded:connect(Enter)