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

How do I fix this health game-pass script?

Asked by 5 years ago
Edited 5 years ago

Hi, I'm currently trying to make a health game-pass, but it doesn't seem to be working, When I go into text and developer console, it says

attempt to concatenate local 'humanoid'  (a nil value)

The script is server sided if your wondering, here is the script:

Service = game:GetService("MarketplaceService")
gamePassId = 5207158
game.Players.PlayerAdded:Connect(function(player)
    local userId = player.UserId
    if Service:UserOwnsGamePassAsync(userId, gamePassId) then
        print("Player owns Health pass")
        local humanoid = game.Workspace:FindFirstChild(player.Name)
        if humanoid == nil then
            print("Failure to set health to " .. humanoid)
        else
            humanoid.Humanoid.MaxHealth = 150
            humanoid.Humanoid.Health = 150
            while true do
                if humanoid.Humanoid.Health <= 0 then
                    wait(7)
                    humanoid.Humanoid.MaxHealth = 150
                    humanoid.Humanoid.Health = 150
                else
                    print("Humanoid is fine, Continue")
                end
            end
        end
    end
end)

If you are wondering what this does, The script gives the Humanoid and extra 50 health when they spawn. If you can help, It'll be greatly appreciated.

1 answer

Log in to vote
0
Answered by 5 years ago

The code will creat this error every time due to the code:-

local humanoid = game.Workspace:FindFirstChild(player.Name)
if humanoid == nil then
    print("Failure to set health to " .. humanoid) -- error
else
-- other code

As you check that the variable humanoid is nil you then concatenate the string Failure to set health to with humanoid which is nil causing this error each time.

print accepts a varying number of arguments allowing you to list the thing you want to print.

print("Failure to set health to ", humanoid) -- no error
print(typeof(tostring(nil))) -- nil to string representation

The code above will not error as tostring will be called on each argument passed to the print function.

With this in mind the error can be resolved but your code itself can be simplified by using the correct events such as CharacterAdded.

-- use local variables and functions where possible
local marketPlaceServ = game:GetService("MarketplaceService")
local gamePassId = 5207158

game.Players.PlayerAdded:Connect(function(plr)

    -- check for game pass
    if marketPlaceServ:UserOwnsGamePassAsync(plr.UserId, gamePassId) then
        print(plr.Name, " Has game pass ", gamePassId)

        -- connect a player added event to give the game pass hp
        plr.CharacterAdded:Connect(function(charModel)
            local humanoid = charModel:WaitForChild("Humanoid")
            humanoid.MaxHealth = 150
            humanoid.Health = humanoid.MaxHealth -- an easy way to give max hp
        end)
    end
end)

I hope this helps.

0
Tysm Starnamics 24 — 5y
Ad

Answer this question