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

Why isn't my sell script changing the leaderstats?

Asked by 5 years ago

I'm trying to make a simulator game and I've started by making a sell pad that's supposed to trade your Breaths for Money (1:1 ratio). When I step on the sell pad, the money should be set to the breaths and the breaths should be set to 0. I added print statements in the function to troubleshoot so I know that the Touched event is working but the leaderstats aren't changing. I even printed the leaderstats to make sure that they weren't changing. Sell Platform Script:

01local part = script.Parent
02part.Touched:Connect(function(part)
03     if (game.Players:GetPlayerFromCharacter(part.Parent))
04    then
05        print("Touched by player!")
06        local player = game.Players:GetPlayerFromCharacter(part.Parent)
07        player.leaderstats.Money.Value = player.leaderstats.Breaths.Value
08        player.leaderstats.Breaths.Value = 0
09    end
10end)

Leaderstats Script:

01function playerJoin(player)
02    local leaderstats = Instance.new("Folder")
03    leaderstats.Name = "leaderstats"
04    leaderstats.Parent = player
05    local breaths = Instance.new("IntValue")
06    breaths.Name = "Breaths"
07    breaths.Value = 0
08    breaths.Parent = leaderstats
09    local money = Instance.new("IntValue")
10    money.Name = 'Money'
11    money.Value = 0
12    money.Parent = leaderstats
13end
14game.Players.PlayerAdded:Connect(playerJoin)
0
are both script's server sided? BradNewTypical 232 — 5y
0
Yes, they're both scripts. The leaderstats is in ServerScriptStorage and the Sell Platform script is in a Part cucucu0001 35 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Here's the problem. You need to put both codes in the same script because what is defined as "leaderstats" are different from both else, you need to use RemoteEvent like what I did below. For more info about RemoteEvent, please check Roblox DevForum. I've reformatted the code for you. Please take some time to read and understand.

01local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
02local part = script.Parent
03 
04game:GetService("Players").PlayerAdded:Connect(function(Player) -- you can just do like this so everytime a player joins
05    local leaderstats = Instance.new("Folder")
06    leaderstats.Name = "leaderstats"
07    leaderstats.Parent = Player
08    local breaths = Instance.new("IntValue")
09    breaths.Name = "Breaths"
10    breaths.Value = 0
11    breaths.Parent = leaderstats
12    local money = Instance.new("IntValue")
13    money.Name = 'Money'
14    money.Value = 0
15    money.Parent = leaderstats
16    part.Touched:Connect(function()
17        RemoteEvent:FireClient(Player)
18    end)
19end

Please add another LocalScript at in StarterPack and delete the Sell Platform Script.

01local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
02local part = script.Parent
03local Player = game.Players.LocalPlayer
04local Touched = false -- Add a debounce so player don't spam
05 
06RemoteEvent.OnClientEvent:Connect(function()
07    if Player.Character:FindFirstChild("Right Leg") then -- I'm not sure if both HumanoidRootPart's and Torso's Legs are called "Right Leg" and "Left Leg". You can check them and change accordingly by testing in Studio > Workspace(When testing) > Your username > (Name of right leg and left leg)
08        if Touched == false then
09            Touched = true
10            print("Touched by player!")
11            local player = script.Parent.Parent
12            player.leaderstats.Money.Value = player.leaderstats.Breaths.Value
13            player.leaderstats.Breaths.Value = 0
14            Touched = false
15        end
View all 26 lines...
0
Where is the first script supposed to go? cucucu0001 35 — 5y
0
Delete the "Sell Platform" Script as i've said in the comment guest_20I8 266 — 5y
0
How are the leaderstats going to update if it's in a LocalScript? cucucu0001 35 — 5y
0
it automatic updates after you touch the sell platform. Or you can use remoteEvents to loop. Refer to: https://scriptinghelpers.org/guides/server-client-relationship-in-roblox (The bottom of the guide). guest_20I8 266 — 5y
0
automatically* guest_20I8 266 — 5y
Ad

Answer this question