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

Leaderstat thingy not working correctly, no errors in output?

Asked by 2 years ago
Edited 2 years ago

I have a script that makes it so that when one of the stats, insanity, reaches 100, it makes things go all crazy and then the player dies. When I went to test it, it didn't do anything. I went to the output, but it said absolutely nothing. No errors. Script is below. (Also, the insanity increases every 1.5 seconds.)

game.Players.ChildAdded:connect(function(player)
wait(1)
local stats = player.leaderstats
local insanity = stats.Insanity
    local scawy = game.Lighting.scarystuff
    local scawy2 = game.Lighting.scaryblur
    local beat = game.Workspace.insanitbeat
        if insanity.Value >= 100 then
            beat:Play()
            scawy.Enabled = true
            scawy2.Enabled = true
            wait(8)
            player.Character.Humanoid.Health = 0
        else
            beat:Stop()
            scawy.Enabled = false
            scawy2.Enabled = false  
    end
end)

Can somebody tell me how to fix this?

1 answer

Log in to vote
0
Answered by
Miniller 562 Moderation Voter
2 years ago
Edited 2 years ago

Debugging your code is very important and every programmer must do it when their code doesn't work. It can be as simple as printing messages so you know that part of the code was successfully executed.

In this case you should have a lower level understanding of the code.

When a player joins, your code will pause for 1 second, declare some variables, and then right after that it will check the value if insanity. It's not giving you output because 1 second after you join your insanity is probably not at 100.

The issue is that right now, 1 second after the player joins, you check the value of insanity, only once, and that's it.

One solution would be listening for the IntValue.Changed event which is fired whenever the value of the IntValue is changed. Then you will check if it's a 100 or more. (Read more about events here, or read the code sample on the first link)

Another solution is to add extra code wherever you change the value of the IntValue. Use this if you only have a few lines of code that changes that value. The extra code would of course be a check whether the value is 100 or more.

Also please use PlayerAdded instead of ChildAddedwhen dealing with the Playersclass.

Ad

Answer this question