Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why doesn't this pad sell my leaderstats?

Asked by
Zottic 19
5 years ago

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)
0
btw im sure its called Coins and Chemicals Zottic 19 — 5y
0
and i if i add a print it also doesnt work Zottic 19 — 5y
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. cherrythetree 130 — 5y

4 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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!?

0
, how would that make it work with filtering enabled? theking48989987 2147 — 5y
0
It's not difficult at all my guy. This is basic scripting xD. kittonlover101 201 — 5y
0
are you trying to woosh? Because it seems like you are theking48989987 2147 — 5y
0
No. It's essential for scripters to know how to use remote events. kittonlover101 201 — 5y
View all comments (5 more)
0
that isn't how you should use them, that wouldn't work with filtering enabled theking48989987 2147 — 5y
0
wdym a a child of backpack, and how do i put it in a player Zottic 19 — 5y
0
Hummm? Wut. Wut the YUCKARI!? kittonlover101 201 — 5y
0
Put the local script into the backpack. kittonlover101 201 — 5y
0
why do u need 2 use remote events, m8 do everything on the server EXpodo1234ALT 18 — 5y
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

0
what's with the slashes User#24403 69 — 5y
0
To space them out, the dashes changed the font so... SerpentineKing 3885 — 5y
Log in to vote
-1
Answered by 5 years ago
Edited 5 years ago

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)
0
doesn't explain the problem, and still won't work theking48989987 2147 — 5y
0
It will are you using a local script Dalbertjdplayz 37 — 5y
0
F I L T E R I N G E N A B L E D theking48989987 2147 — 5y
0
nope doesnt work Zottic 19 — 5y
View all comments (3 more)
0
whats the error Dalbertjdplayz 37 — 5y
0
doesnt display an error Zottic 19 — 5y
0
F I L T E R I N G E N A B L E D kittonlover101 201 — 5y

Answer this question