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:
local part = script.Parent part.Touched:Connect(function(part) if (game.Players:GetPlayerFromCharacter(part.Parent)) then print("Touched by player!") local player = game.Players:GetPlayerFromCharacter(part.Parent) player.leaderstats.Money.Value = player.leaderstats.Breaths.Value player.leaderstats.Breaths.Value = 0 end end)
Leaderstats Script:
function playerJoin(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local breaths = Instance.new("IntValue") breaths.Name = "Breaths" breaths.Value = 0 breaths.Parent = leaderstats local money = Instance.new("IntValue") money.Name = 'Money' money.Value = 0 money.Parent = leaderstats end 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.
local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent") local part = script.Parent game:GetService("Players").PlayerAdded:Connect(function(Player) -- you can just do like this so everytime a player joins local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = Player local breaths = Instance.new("IntValue") breaths.Name = "Breaths" breaths.Value = 0 breaths.Parent = leaderstats local money = Instance.new("IntValue") money.Name = 'Money' money.Value = 0 money.Parent = leaderstats part.Touched:Connect(function() RemoteEvent:FireClient(Player) end) end
Please add another LocalScript at in StarterPack and delete the Sell Platform Script.
local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent") local part = script.Parent local Player = game.Players.LocalPlayer local Touched = false -- Add a debounce so player don't spam RemoteEvent.OnClientEvent:Connect(function() 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) if Touched == false then Touched = true print("Touched by player!") local player = script.Parent.Parent player.leaderstats.Money.Value = player.leaderstats.Breaths.Value player.leaderstats.Breaths.Value = 0 Touched = false end elseif Player.Character:FindFirstChild("Left Leg") then if Touched == false then Touched = true print("Touched by player!") local player = script.Parent.Parent player.leaderstats.Money.Value = player.leaderstats.Breaths.Value player.leaderstats.Breaths.Value = 0 Touched = false end end end)