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

[SOLVED] Leaderstats value not decreasing?

Asked by 5 years ago
Edited 4 years ago

I am currently working on a shop system and I came accross this issue. Here's my script.

local CoolDown = true
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:WaitForChild('Humanoid') then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local PlayerGui = player.PlayerGui
        if CoolDown == true then
            CoolDown = false
            PlayerGui.ShopGui.TextLabel:TweenPosition(UDim2.new(0.5,0,0.5,0),'Out','Sine',.5)
            PlayerGui.ShopGui.TextLabel.Text = "Are you sure you want to purchase this item for $"..script.Parent.Price.Value.."?"
            wait(2)
            CoolDown = true
            PlayerGui.ShopGui.TextLabel.Yes.MouseButton1Click:Connect(function()
                if player.leaderstats.Yen.Value > 150 then
                    PlayerGui.ShopGui.TextLabel:TweenPosition(UDim2.new(0.5,0,1.5,0),'In','Sine',.5)
                    player.leaderstats.Yen.Value = player.leaderstats.Yen.Value - script.Parent.Price.Value
                end
            end)
            PlayerGui.ShopGui.TextLabel.No.MouseButton1Click:Connect(function()
                PlayerGui.ShopGui.TextLabel:TweenPosition(UDim2.new(0.5,0,1.5,0),'In','Sine',.5)
            end)
        end
    end
end)

Everything is supposed to work, but it's not. The items are not being purchased. Any help?

0
Errors? ankurbohra 681 — 5y
0
There are none. It's just not showing any errors. Sensei_Developer 298 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

You cannot access descendants of Player.PlayerGui (neither their events) from the server since these are local instances and are non-existent to the server. To fix this, you will have to use a RemoteEvent and use that to change your UI on the client. Moving it as a whole to the client is a sure fix.

0
But I'll try anyways. Thanks. Sensei_Developer 298 — 5y
0
It worked! Thank you! Sensei_Developer 298 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

When ankurbohra04 said his answer, I tried it and it didn't work. Any perhaps I did something wrong? This is my updated scripts.

Server side script:

game.Players.PlayerAdded:Connect(function(plr)
    local CoolDown = true
    print(plr:WaitForChild('leaderstats'):WaitForChild('Yen').Value)
    script.Parent.Touched:Connect(function(hit)
        if hit.Parent:WaitForChild('Humanoid') then
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            local PlayerGui = player.PlayerGui
            if CoolDown == true then
                CoolDown = false
                game.ReplicatedStorage.ConfirmPurchase:FireClient(player)
                wait(2)
                CoolDown = true
                PlayerGui.ShopGui.TextLabel.Yes.MouseButton1Click:Connect(function()
                    if player:WaitForChild('leaderstats'):WaitForChild('Yen').Value >= 150 then
                        game.ReplicatedStorage.CloseGui:FireClient(player)
                        plr:WaitForChild('leaderstats'):WaitForChild('Yen').Value = plr:WaitForChild('leaderstats'):WaitForChild('Yen').Value - 150
                    end
                end)
                PlayerGui.ShopGui.TextLabel.No.MouseButton1Click:Connect(function()
                    game.ReplicatedStorage.CloseGui:FireClient(player)
                end)
            end
        end
    end)
end)

Here's my localscript:

game.ReplicatedStorage.ConfirmPurchase.OnClientEvent:Connect(function()
    script.Parent:TweenPosition(UDim2.new(0.5,0,0.5,0),'Out','Sine',.5)
    script.Parent.Text = "Are you sure you want to purchase this item for $"..workspace.CommonEgg.Price.Value.."?"
end)
game.ReplicatedStorage.CloseGui.OnClientEvent:Connect(function()
    script.Parent:TweenPosition(UDim2.new(0.5,0,1.5,0),'Out','Sine',.5)
end)

Can someone else help please?

EDIT: It worked! Thank you so much! It turns out that it only worked in-game, not in studio test mode. Once again, I cannot thank you enough for this, ankurbo04!

0
Excellent ankurbohra 681 — 5y
0
I added a script to my part- on - an - axis question AltNature 169 — 4y

Answer this question