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

Touch event not changing leaderstats?

Asked by
zomspi 541 Moderation Voter
4 years ago

I have a script where when I touch a part it does a bunch of stuff, all of this works except changing the leaderstats of the player, can anyone tell me why this is not working?

Main script

game.Players.PlayerAdded:Connect(function(plr)
    script.Parent.Touched:Connect(function(hit)
        if game.Players:GetPlayerFromCharacter(hit.Parent) then
            if workspace:FindFirstChild("Walls", true) then
                local wall = workspace.Walls:FindFirstChild("Wall4", true)
                wall.CanCollide = true
                wall.BrickColor = BrickColor.new("Really red")
                script.Parent.CanCollide = false
                script.Parent.BrickColor = BrickColor.new("Really red")
                game.ReplicatedStorage.NightSky.Parent = game.Lighting

                if plr.leaderstats.Stage.Value >= 0  then
                    plr.leaderstats.Stage.Value = 1
                else

                end

                wait(15)
                if workspace:FindFirstChild("Walls", true) then
                     local Wall1 = workspace.Walls:FindFirstChild("Wall5", true)
                     Wall1.CanCollide = false
                     Wall1.BrickColor = BrickColor.new("Lime green")
                end
            end
end
end)
end)



Part that isn't working

if plr.leaderstats.Stage.Value >= 0  then
                    plr.leaderstats.Stage.Value = 1
                else

                end

2 answers

Log in to vote
0
Answered by
pwx 1581 Moderation Voter
4 years ago

You do not need PlayersAdded for this.

Try on line 03:

local plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- thats your player define
if plr then -- check if player
-- rest code here
end
0
that didn't work zomspi 541 — 4y
0
Any errors? pwx 1581 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Re-posting revised answer. Going off the comments you gave, I came up with this. Try this, if it doesn't work then I might be at a loss.

script.Parent.Touched:Connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr and plr:FindFirstChild("leaderstats") then
        if workspace:FindFirstChild("Walls", true) then
            local wall = workspace.Walls:FindFirstChild("Wall4", true)
            wall.CanCollide = true
            wall.BrickColor = BrickColor.new("Really red")
            script.Parent.CanCollide = false
            script.Parent.BrickColor = BrickColor.new("Really red")
            game.ReplicatedStorage.NightSky.Parent = game.Lighting

            if plr.leaderstats.Stage.Value <= 0  then
                plr.leaderstats.Stage.Value = 1
            end

            wait(15)
            if workspace:FindFirstChild("Walls", true) then
                 local Wall1 = workspace.Walls:FindFirstChild("Wall5", true)
                 Wall1.CanCollide = false
                 Wall1.BrickColor = BrickColor.new("Lime green")
            end
        end
    end
end)
0
sorry, that doesn't work either zomspi 541 — 4y

Answer this question