Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why isn't my sell script changing the leaderstats?

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)

2 comments

0

are both script's server sided?

0

Yes, they're both scripts. The leaderstats is in ServerScriptStorage and the Sell Platform script is in a Part

1 answer

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)

5 comments

0

Where is the first script supposed to go?

0

Delete the "Sell Platform" Script as i've said in the comment

0

How are the leaderstats going to update if it's in a LocalScript?

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).

0

automatically*

This discussion is preserved as it was. New answers, comments, and votes are closed.