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

Why won't this script take away money from players account?

Script in ServerScriptService

local event1 = game.ReplicatedStorage:WaitForChild("Events").PurchaseStorage
local Money= game.Players.LocalPlayer.Money

game.ReplicatedStorage.Events.PurchaseStorage.OnServerEvent:Connect(function(plr, event1)
	if Money.Value >= 10000 then
		Money.Value = Money.Value - 10000
else
	if Money.Value <= 9999 then
		end
	end
end)

Localscript

local evnt = game.ReplicatedStorage:WaitForChild("Events").PurchaseStorage
local plr = game.Players.LocalPlayer

script.Parent.MouseButton1Down:Connect(function()
	if plr.Money.Value >= 10000 then
			script.Parent.Parent.Parent.Parent.Parent.Parent.Success.Visible = true
				wait(3)
			script.Parent.Parent.Parent.Parent.Parent.Parent.Success.Visible = false
		evnt:FireServer("03X")
	elseif
		plr.Money.Value <= 9999 then
			script.Parent.Parent.Parent.Parent.Parent.Parent.Error.Visible = true
			wait(3)
			script.Parent.Parent.Parent.Parent.Parent.Parent.Error.Visible = false
	end
end)

Any idea why this does nothing to the Money value?

game.Players.PlayerAdded:connect(function(plr)	
	local Money = Instance.new("IntValue", plr)
	Money.Name = "Money"
	Money.Value = 1000
	
	local rubys = Instance.new("IntValue", plr)
	rubys.Name = "Rubys"
	rubys.Value = 10
end)

9 comments

0

the game wont let u troll the players

0

where is the localscript in?

0

the script is in a button in a GUI

0

hey Vxpper can you post your leaderstats script too if you can do that i might be able to figure it out because i deal wth these types of scripts while working on my simulator game

0

I will add it, the only problem is that it won

0

won't save or that script is just broken, I'll post one with just the basic values I wanna make a script that saves the value

0

but i can't seem to make them save so im just stuck at the moment

0

ok hold on im working something up ill give you the script with tips and stuff just hold on a few min :)

0

if you have any trouble with the scripts just ask ill gladly help, also if you need to know how you can award the payer something from buying Storage i can help with that other then that good luck :)

1 answer

✓ Accepted answer

Hopefully this can help you with your scripts also make sure to read little notes i have left on the script :)

first the leaderboard script

--this is the leaderboard its auto save so dont worry
local datastore = game:GetService("DataStoreService")--/                           		 \
local MyMoney = datastore:GetDataStore("MoneyData")--getting DataStoreService to store Data |
local MyRuby = datastore:GetDataStore("RubyData")--------\                                 /

game.Players.PlayerAdded:connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"-- making a folder and naming it leaderstats to store money and Ruby in.
local Money = Instance.new("IntValue", leaderstats)
Money.Name = "Money" -- Money
local Ruby = Instance.new("IntValue", leaderstats)
Ruby.Name = "Ruby's" -- Ruby
Money.Value = MyMoney:GetAsync(player.UserId) or 0 -- player joins give his amount of saved Money or set to 0 if its tha players first time
MyMoney:SetAsync(player.UserId, Money.Value) -- setting the players data as he joins
Ruby.Value = MyRuby:GetAsync(player.UserId) or 0
MyRuby:SetAsync(player.UserId, Ruby.Value)

game.Players.PlayerRemoving:connect(function() -- playing functon when the player leaves
MyMoney:SetAsync(player.UserId, Money.Value) -- setting the players money for when he joins this is the amount he will be given
MyRuby:SetAsync(player.UserId, Ruby.Value)
end)
end)

this leaderboard has 2 values Money and Ruby they both auto save when the player leaves the game

next will be the script in ServerScriptService which is used on a function to take away the players money and give him his item or what ever you desire

game.ReplicatedStorage.Events.PurchaseStorage.OnServerEvent:Connect(function(player)-- function playing on PurchaseStorage
	print("Working!") -- print working to know if the function has been played
    if player.leaderstats.Money.Value >= 10000 then -- checks if the player has enough money
        player.leaderstats.Money.Value = player.leaderstats.Money.Value - 10000 -- if he does subtract money
print("Enough Money!") -- if has enough money print Enough Money to check if teh script is working
else if player.leaderstats.Money.Value < 9999 then
	print("Not Enough Money") -- if player doesnt have enough, it does nothing and prints "Not Enough Money"
        end
    end
end)

if youve noticed in the Buying script i removed the local variables such as local money = game.players.localplayers -- etc... and that because these can only work in a local script and you can get the Game.Players.Localplayers part from the player at the end of the fire server line after function after that you can simply put leaderstats.Money.Value

hopefully this helped you accomplish what ever you were trying to seek if you have any problems or question about the scripts or any other script just ask :)

6 comments

0

Thank you! Still experiencing some problems with the saving, my money and ruby values are in the player instead of leaderstats, Im adding a value into the player and that

0

but now saving and subtracting money will not work either

0

Saving does work when I go from studio to in-game but still doesn't subtract money

0

Vxpper try looking at what the errors are, if there are any warning then can you tell me what they are, also i tested the script in my own studio and it should work unless it doesnt know when the event is fired.

0

I don't think it knows when the event is fired

0

oh make sure you have the events in the right place

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