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:
01 | local part = script.Parent |
02 | part.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 |
10 | end ) |
Leaderstats Script:
01 | function 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 |
13 | end |
14 | game.Players.PlayerAdded:Connect(playerJoin) |
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.
01 | local RemoteEvent = game:GetService( "ReplicatedStorage" ):WaitForChild( "RemoteEvent" ) |
02 | local part = script.Parent |
03 |
04 | game: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 ) |
19 | end |
Please add another LocalScript at in StarterPack and delete the Sell Platform Script.
01 | local RemoteEvent = game:GetService( "ReplicatedStorage" ):WaitForChild( "RemoteEvent" ) |
02 | local part = script.Parent |
03 | local Player = game.Players.LocalPlayer |
04 | local Touched = false -- Add a debounce so player don't spam |
05 |
06 | RemoteEvent.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 |