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

Script isn't properly subtracting and adding stats?

So I have made a script that is suppose to take away Candy from the player and give them Cash instead, it is located in a button inside a gui, however, it doesn't seem to work properly. It takes away the candy and adds money, but it does not save.

I initially thought it was an issue with the datastore but it turns out that the issue is within the script, thanks =^)

player = game.Players.LocalPlayer:findFirstChild("leaderstats")
money2 = player:findFirstChild("Moneys")
money = player:findFirstChild("Candy")
price = 6
extra = 2

text = {	"Here you go! Would you like to trade more candy?"}


function Click()
	wait()
if money.Value >= price then
money.Value = money.Value - price
money2.Value = money2.Value + extra
local randText = text[math.random(#text)]
for a=1,string.len(randText) do
script.Parent.Parent.Dialog.Text = string.sub(randText,1,a)..""
wait(.01)
end
end
end
script.Parent.MouseButton1Down:connect(Click)

2 answers

Judging by the variables, I'm assuming this is inside of a server script. You can only use LocalPlayer within a LocalScript, because it is trying to define the player. It is also trying access the leaderstats property within the player.

So, try putting the code in a LocalScript and settings the variables to local as well, like this.

local player = game.Players.LocalPlayer:FindFirstChild("leaderstats")
local money2 = player:findFirstChild("Moneys")
local money = player:findFirstChild("Candy")
local price = 6
local extra = 2

local text = {    "Here you go! Would you like to trade more candy?"}

function Click()
    wait()
if money.Value >= price then
money.Value = money.Value - price
money2.Value = money2.Value + extra
local randText = text[math.random(#text)]
for a=1,string.len(randText) do
script.Parent.Parent.Dialog.Text = string.sub(randText,1,a)..""
wait(.01)
end
end
end
script.Parent.MouseButton1Down:connect(Click)

Also check for any output errors as well.

1 comment

0

It doesn't change it unfortunately. A note to add on, when it takes away the candy, if the player gets more candy later on, it will combine it to the previous amount. For example, if I had 90 candy and I got rid of that and exchanged it for money but then soon got 6 candy, it would give me 96 instead of just 6.

First and foremost, make this a LocalScript inside the GuiButton if it isn't.

And secondly, your problem is probably that the player does not own a leaderstats, Moneys, or Candy object yet. Use WaitForChild.

And for the efficiency fixes..

  • tab your code correctly.

  • connect is deprecated - use Connect.

  • You can use the # operator to get the length of a string.


For this to replicate, you have to use remotes :)

Use the FireServer function from the client, and execute the code on the server in an OnServerEvent event.

LocalScript:

local remote = game.ReplicatedStorage.TradeCandy --This is your remote
local q = "Would you like to trade more candy?"
local price,extra,text = 6,2,{
	"Here you go!";
	"Omnom.. thank you for your contributions.";
	"Heck yeah keep the candy train rolling!";
}


function Click()
	if money.Value >= price then
		local randText = text[math.random(#text)]..q
		remote:FireServer(price,extra,randText); --FireServer
	end
end

script.Parent.MouseButton1Down:Connect(Click) --Use Connect!

Script in ServerScriptStorage:

local remote = game.ReplicatedStorage.TradeCandy --This is your remote

remote.OnServerEvent:Connect(function(plr,cost,award,msg)
	local money2 = plr:WaitForChild("Moneys") --Use WaitForChild
	local money = plr:WaitForChild("Candy")
	money.Value = money.Value - cost
	money2.Value = money2.Value + award
	for a = 1, #msg do --# operator!
		script.Parent.Parent.Dialog.Text = msg:sub(1,a)
		wait()
	end
end)

3 comments

0

It subtracts the candy and adds money, however, if the player collects more candy, it adds onto the previous amount. For example, if I traded 50 candy and I collect 6 candy later, instead of giving me 6 candy, it returns the whole 50 candy, giving me 56 candy.

0

Ahhh that's due to FE - i'll edit.

0

Still has the same issue with the candy as I mentioned earlier.

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