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)
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
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!?
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 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. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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)