/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3 comments
btw im sure its called Coins and Chemicals
and i if i add a print it also doesnt work
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.