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

Script still adds to the leaderboard value even though a condition isnt met?

Asked by 4 years ago

I have made a script to make this orb only add 1 to your stats one time. It works if I do not tweak the specified value in orb folder, however if I attempt to change the value manually in test mode, it still awards an orb collected. The orbfolder has a datastore to it, but it doesnt seem to be the problem as I tested it in studio changing the value myself with the orb still being awarded. If anyone could identify what is wrong, I would appreciate it. My code is below:

game.Players.PlayerAdded:Connect(function(join)
script.Parent.Touched:Connect(function(plr)
    local humanoidcheck = plr.Parent:FindFirstChild("Humanoid")
    if humanoidcheck ~= nil then
        local orbfolder = join.orbfolder
        if orbfolder.orb1.Value == 0 then
        local orbs = join.leaderstats.Orbs
        orbs.Value = orbs.Value + 1
        orbfolder.orb1.Value = 1
    end
    end
    end)
end)

Thank you!

0
Delete the PlayerAdded event. See if that helps IProgram_CPlusPlus 58 — 4y
0
It will break the entire script as I need that to find the player's orbfolder. spot6003 64 — 4y
0
He is correct, remove the PlayerAdded event and use game.Player:GetPlayerFromCharacter(plr.Parent) to get the player instead darkelementallord 686 — 4y
0
game.Players* darkelementallord 686 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

game.Players.PlayerAdded is unneeded here. At the moment your script will create a new touched event every time a Player joins, and hence will run the code every time it is touched for each Player.

What you want to do is to use :GetPlayerFromCharacter(char) to get the player instead:

script.Parent.Touched:Connect(function(part)
    local humanoid = part.Parent:FindFirstChild('Humanoid')
    if (humanoid) then
        local player = game.Players:GetPlayerFromCharacter(humanoid.Parent);
        if (player) then
            -- Run your code
        end
    end
end)
0
Alright, thank you! Will the way you wrote the part to check for humanoid and player work? spot6003 64 — 4y
0
Yes, when using object references in an 'if' statement, it will always return true if the object exists. darkelementallord 686 — 4y
0
So this still doesnt fix the problem where it still adds even when the condition for orb1 is false spot6003 64 — 4y
0
Your code looks correct on that front. It should only add to the value if join.orbfolder.orb1.Value is equal to 0. Maybe check your hierarchy is correct? darkelementallord 686 — 4y
0
I believe its correct, there is no error in the console spot6003 64 — 4y
Ad

Answer this question