Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why won't my Script print a message when player is killed or kills?

Asked by 2 years ago

I made a Script in ServerScriptStorage, it's deposed to detect if a player kills or gets killed and print when these events happen. The sword creates a creator tag in the humanoid but my script won't print the message. What's the problem?

Script:

local players = game.GetService("Players")

players.PlayerAdded:Connect(function(player)

    player.CharacterAdded:Connect(function(character)

        local humanoid = character:WaitForChild("Humanoid")
        humanoid.Died:Connect(function()
            print("You died!")
            if humanoid:FindFirstChild("creator") ~= nil then
                local Killer = humanoid.creator.Value
                print("You're the killer!")
            end

        end)
    end)
end)

3 answers

Log in to vote
0
Answered by 2 years ago

Maybe try putting it in ServerScriptService.

local players = game.GetService("Players")

players.PlayerAdded:Connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoid = character:WaitForChild("Humanoid")
    local creator  = humanoid:FindFirstChild("creator")

    humanoid.Died:Connect(function()
        print("You died!")
    end)

    creator.Changed:Connect(function()
        if creator.Value ~= nil then
            local Killer = creator.Value
            print("You're the killer!")
        end
    end)
end)

I hope this helped!

Ad
Log in to vote
0
Answered by 2 years ago

The problem is in your first line of code, try this:

local players = game:GetService("Players")
Log in to vote
0
Answered by 2 years ago

you made a error on the first line

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)

    player.CharacterAdded:Connect(function(character)

        local humanoid = character:WaitForChild("Humanoid")
        humanoid.Died:Connect(function()
            print("You died!")
            if humanoid:FindFirstChild("creator") ~= nil then
                local Killer = humanoid.creator.Value
                print("You're the killer!")
            end

        end)
    end)
end)

try this script

Answer this question