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

Why doesn't this pad sell my leaderstats?

I have made a sell pad that sells chemicals and gives money on touch, can someone explain why it isnt working? btw its in a local player

local chemicals = game.Players.LocalPlayer.leaderstats.Chemicals.Value
local coins  = game.Players.LocalPlayer.leaderstats.Coins.Value

function onTouch(hit)
	chemicals = chemicals - chemicals
	coins = coins + chemicals
end

script.Parent.Touched:Connect(onTouch)

3 comments

0

btw im sure its called Coins and Chemicals

0

and i if i add a print it also doesnt work

0

Instead of subtracting them, use 0 instead. That also happened to me. Also because chemicals has no value, coins is just getting 0 + it's own value. I bet you are making an simulator or tycoon game, I think? Just make the coin's value before chemical's value is deleted.

4 answers

✓ Accepted answer

Issues


Couple of things here:

  • You are using references instead of values, which dont update when the actual object changes

  • You are modifying objects from the client, which won't replicate to the server

  • You have a local script located outside of the client, which means it won't run


Solution


The simplest way to do this is by doing it on the server, as Touched events can run on the server aswell.

You may be asking: how would you get the chemical values and coins values on the server?

Well, as the first parameter of the touched event is that part that it touched, Which, if it is a player, it generally will be a bodypart.

Now, you can use the GetPlayerFromCharacter function to get the player from the character, then get the values you need to change, like such:

--server(regular) script that is the child of the part in question

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr then
		local chemicals = plr.leaderstats.Chemicals
		local coins = plrleaderstats.Coins
		coins.Value = coins.Value + chemicals.Values
		chemicals.Value = 0
	end
end)

Hopefully this helped! be sure to accept if it did

If you want this to work properly use FE (Filtering Enabled).

To do this use an remote event.

Put the event in replicated storage.

Name the event MoneyChangeEvent.

Also your question doesn't explain anything.

SCRIPT (make it a child of the block that is going to be touched):

wait(1)

function onTouch(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	game.ReplicatedStorage.MoneyChangeEvent:FireClient(plr)
end

script.Parent.Touched:Connect(onTouch)

LOCAL SCRIPT (make it a child of backpack or somewhere else on the player):

function OnServerThing()
	local plr = game.Players.LocalPlayer
	local chemicals = game.Players.LocalPlayer.leaderstats.Chemicals
	local coins  = game.Players.LocalPlayer.leaderstats.Coins
	
	coins.Value = coins.Value + chemicals.Value --// change this if the coins and chemicals aren't in leaderstats.
	chemicals.Value = 0 --// change this if the coins and chemicals aren't in leaderstats.
end

script.Parent.Touched:Connect(OnServerThing)

EDIT: What do you mean it's in a local script!?

9 comments

0

, how would that make it work with filtering enabled?

0

It's not difficult at all my guy. This is basic scripting xD.

0

are you trying to woosh? Because it seems like you are

0

No. It's essential for scripters to know how to use remote events.

0

that isn't how you should use them, that wouldn't work with filtering enabled

0

wdym a a child of backpack, and how do i put it in a player

0

Hummm? Wut. Wut the YUCKARI!?

0

Put the local script into the backpack.

0

why do u need 2 use remote events, m8 do everything on the server

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Changing the Stats on the Client

If you only want to change the stats per player, you need to place the .Value part of each variable within the function, since when you originally defined the "chemicals" and "coins", these values were outside the code, and thus would remain the same regardless (they are properties of instances)

REVISED LOCAL SCRIPT

local player = game:GetService("Players").LocalPlayer
local chemicals = player:WaitForChild("leaderstats"):WaitForChild("Chemicals")
local coins  = player:WaitForChild("leaderstats"):WaitForChild("Coins")

script.Parent.Touched:Connect(function(hit)
	coins.Value = coins.Value + chemicals.Value
	chemicals.Value = 0
end)

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Changing the Stats for All Players (Remote Event)

Filtering Enabled is meant to prevent replication from the Client to the Server. If this was disabled, the above code would work fine, however, since it is generally preferred to leave it on, you will require a RemoteEvent to communicate between the Server and the Client

Server Script under Server Script Service

local remote = Instance.new("RemoteEvent")
remote.Name = "StatChange"
remote.Parent = game:GetService("ReplicatedStorage")

remote.OnServerEvent:Connect(function(player)
	local chemicals = player:WaitForChild("leaderstats"):WaitForChild("Chemicals")
	local coins = player:WaitForChild("leaderstats"):WaitForChild("Coins")
	
	coins.Value = coins.Value + chemicals.Value
	chemicals.Value = 0
end)

Local Script under StarterGui

local remote = game:GetService("ReplicatedStorage"):WaitForChild("StatChange")
local part = workspace.Part

part.Touched:Connect(function(hit)
	remote:FireServer()
end)

For this scenario, the "part" is the location of your original "script.Parent" ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Changing the Stats for All Players (Server-Only)

However, the best method for this would to be simply keeping everything on the server

Server Script under a Part

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		if player then
			local chemicals = player:WaitForChild("leaderstats"):WaitForChild("Chemicals")
			local coins = player:WaitForChild("leaderstats"):WaitForChild("Coins")
			
			coins.Value = coins.Value + chemicals.Value
			chemicals.Value = 0
		end
	end
end)

You also want to place the chemical value removal after the coin value addition since otherwise you are adding zero to the coins. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

2 comments

0

what's with the slashes

0

To space them out, the dashes changed the font so...

How about this

wait(1)
local chemicals = game.Players.LocalPlayer.leaderstats.Chemicals
local coins  = game.Players.LocalPlayer.leaderstats.Coins

function onTouch(hit)
    coins.Value = coins.Value + chemicals.Value
    chemicals.Value = 0
end

script.Parent.Touched:Connect(onTouch)

7 comments

0

doesn't explain the problem, and still won't work

0

It will are you using a local script

0

F I L T E R I N G E N A B L E D

0

nope doesnt work

0

whats the error

0

doesnt display an error

0

F I L T E R I N G E N A B L E D

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