Help with money script

I need help with my script because what I’m trying to do is every time you kill someone you’ll get money and with the money you earn you can spend it in the shop and whatever you spend in the shop it will always save in your inventory.

Problem - It wont give you money when you kill someone and when you buy something from the store in puts you in the negatives and when you die it goes away and you hafto buy it all over again every time.

( ive tried looking it up and i keep getting no results or i cant find what i need )

ShopScript:


local rocketBtn = script.Parent.RocketBtn

local rs = game:GetService("ReplicatedStorage")

local re = rs:WaitForChild("EquipRe")

local item = "RocketLauncher"

local price = 50

local player = game:GetService("Players").LocalPlayer

local function selectRocket()

rocketBtn.BackgroundColor3 = Color3.new(.5, .5, 1)

item = "RocketLauncher"

price = 50

end

local function buyIt()

re:FireServer(item, price)

script.Parent.Visible = false

rocketBtn.BackgroundColor3 = Color3.new(1, 1, 1)

end

buyBtn.Activated:Connect(buyIt)

rocketBtn.Activated:Connect(selectRocket)

Please use the code block like this:

print("Test")

As it makes it easier to read.
Could you please show me your server script?

i understand that but i dont no how to do that

this is my equip script


local re = rs:WaitForChild("EquipRe")

local function giveTool(player, item, price)

local tool = rs:WaitForChild(item):Clone()

local money = player.leaderstats.Money

money.Value = money.Value - price

tool.Parent = player.Backpack

end

re.OnServerEvent:Connect(giveTool)

you can use a code block by putting ``` at the beginning and end

What’s the code you wrote for the combat?

I suggest that you check if the player has enough money before you let them buy it via saying:

if money.Value >= price then
-- Give them their item and take their money.
end

This is my ShopScript:


local rs = game:GetService("ReplicatedStorage")

local re = rs:WaitForChild("EquipRe")

local item = "RocketLauncher"

local price = 50

local player = game:GetService("Players").LocalPlayer

local function selectRocket()

rocketBtn.BackgroundColor3 = Color3.new(.5, .5, 1)

item = "RocketLauncher"

price = 50

end

local function buyIt()

re:FireServer(item, price)

script.Parent.Visible = false

rocketBtn.BackgroundColor3 = Color3.new(1, 1, 1)

end

buyBtn.Activated:Connect(buyIt)

rocketBtn.Activated:Connect(selectRocket)```

this is my equip script

local re = rs:WaitForChild("EquipRe")

local function giveTool(player, item, price)
	local tool = rs:WaitForChild(item):Clone()
	local money = player.leaderstats.Money
	money.Value = money.Value - price
	tool.Parent = player.Backpack
end

re.OnServerEvent:Connect(giveTool)

If you can also show us the Money Kill Script, that would be great. And based on what I see, these are just my assumptions. For starts as to why your money goes into negative, you should check if the player has enough money to purchase the item first. Like this:

if money.Value >= price then
#Stuff
end

Furthermore, another thing I kind of noticed, is that you are getting the price from the Client. Which can easily be altered and abused. The price should always be deducted and decided on the server. And for why you lose your item when you die?

Items in the backpack, besides items in the starterpack, will always be removed. You’ll have to constantly give it back to them with a script. Maybe keep the items they purchased in a table, and cycle through that table with a loop everytime their character is added.

#Side note
It has been a while since I have done anything on Roblox, so I may be wrong in some of my assumptions.

Check if the player’s money is above the price, then give the player a tool
You can also put the tool in StarterGear to prevent player’s from losing it on death

local re = rs:WaitForChild("EquipRe")
local function giveTool(player, item, price)
	local tool = rs:WaitForChild(item):Clone()
	local tool1 = rs:WaitForChild(item):Clone()
	local money = player.leaderstats.Money
	if money.Value >= price then
		money.Value = money.Value - price
		tool.Parent = player.StarterGear
		tool1.Parent = player.Backpack
	end
end

re.OnServerEvent:Connect(giveTool)

I’m sorry but I’m honestly really confused

I will put comments in my code to demonstrate.

local re = rs:WaitForChild("EquipRe")
local function giveTool(player, item, price) -- Make a function
	local tool = rs:WaitForChild(item):Clone() -- Starter gear tool
	local tool1 = rs:WaitForChild(item):Clone() -- Backpack tool
	local money = player.leaderstats.Money
	if money.Value >= price then -- If the money is greater or equal to the price then run other code
		money.Value -= price -- Removing the player's money.
		tool.Parent = player.StarterGear -- Give the player startergear tool so if they die they get it back
		tool1.Parent = player.Backpack -- Then put the tool in backpack so player doesn't need to reset to get it
	end
end

re.OnServerEvent:Connect(giveTool)

where do i hafto put this at for it to work ?

In your equip script to make sure it works

Just put it into a serverscript.

ok so i did put it there put where it says re it has a blue line under it

Oh right. RS isn’t defined in the script. Replace the first line with local re = game.ReplicatedStorage:WaitForChild("EquipRe")

Also, if he is going to take that code you should probably define

rs = game:GetService("ReplicatedStorage")
1 Like

i did replace it but that really didn’t do anything