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

For some reason the math won't work on my buying script?

Asked by 3 years ago

I have an intvalue as Cash

--Variables--
local Item = game.Workspace.Guns.FourthGun:Clone()
local Button = script.Parent
local Player = game.Players.LocalPlayer
local Cost = 50
local Cash = Player.leaderstats.Cash.Value
local Mouse = Player:GetMouse()
local PurchaseButton = script.Parent.BuyPrompt.PurchaseButton
local LeaveButton = script.Parent.BuyPrompt.LeaveButton

--Actual scripting--
script.Parent.MouseButton1Down:Connect(function()
    script.Parent.BuyPrompt.Visible = true
end)

LeaveButton.MouseButton1Down:Connect(function()
    script.Parent.BuyPrompt.Visible = false
end)

PurchaseButton.MouseButton1Down:Connect(function()
    Cash = Cash - Cost
end)

2 answers

Log in to vote
0
Answered by 3 years ago

Never, NEVER, NEVER ever handle player leaderstats and purchases on the client. It makes the job easy for exploiters to give themselves cash. Instead, handle it server side.

--Variables--
local Button = script.Parent
local Player = game:GetService("Players").LocalPlayer
local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent
local PurchaseButton = script.Parent.BuyPrompt.PurchaseButton
local LeaveButton = script.Parent.BuyPrompt.LeaveButton

--Actual scripting--
script.Parent.MouseButton1Down:Connect(function()
    script.Parent.BuyPrompt.Visible = true
end)

LeaveButton.MouseButton1Down:Connect(function()
    script.Parent.BuyPrompt.Visible = false
end)

PurchaseButton.MouseButton1Down:Connect(function()
   RemoteEvent:FireServer("FourthGun")
end)

We will be using RemoteEvent to communicate client to server.

--A server script in ServerScriptService

local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent
local Item = game:GetService("ServerStorage").Guns.FourthGun
--ALWAYS handle clone tools on server side.
RemoteEvent.OnServerEvent:Connect(function(Player, item)
    if item == Item.Name then
        local Cash = Player.leaderstats.Cash
        if Cash.Value >= 50 then
            Cash.Value = Cash.Value - 50
            Item = Item:Clone()
            Item.Parent = Player.Backpack
        end
    end
end)


Read more about RemoteEvents here: https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

I'll explain here what the heck is going on in the script. A client is a player, the server is the game that a client does not replicate to. Many people make the mistake of handling "purchases" client side. You should ALWAYS asume the client is an exploiter. Exploiters can always spoof scripts to give themselves advantages. And you cannot change a player's cash through the client and expect it to save. Datastores are managed server-side, and cannot see what the client is seeing. That means you CANNOT save player's leaderstats client side.

0
Thank You very much! Kittymuffin323 -10 — 3y
0
my script won't get past line 24 Kittymuffin323 -10 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Basically There shouldn't be anything wrong but the script could be confused and I will show how I changed it up. MAKE THIS A SERVER SIDE SCRIPT NOT LOCAL

--Variables--
local Item = game.Workspace.Guns.FourthGun:Clone()
local Button = script.Parent
local lead = game:WaitForChild("leaderstats")
local Player = lead.Parent
local Cost = 50
local Cash = Player.leaderstats.Cash
local Mouse = Player:GetMouse()
local PurchaseButton = script.Parent.BuyPrompt.PurchaseButton
local LeaveButton = script.Parent.BuyPrompt.LeaveButton

--Actual scripting--
script.Parent.MouseButton1Down:Connect(function()
    script.Parent.BuyPrompt.Visible = true
end)

LeaveButton.MouseButton1Down:Connect(function()
    script.Parent.BuyPrompt.Visible = false
end)

PurchaseButton.MouseButton1Down:Connect(function()
    Cash.Value -= Cost--Heres you can do it easier using -= so you dont gotta have cash twice
end)

if this works or helps accept

0
No. This will DEFINITELY not work. PlayerGuis should be handled LOCALLY. "leaderstats" is also not a child of game. It would error. I suggest you edit your answer because of the many flaws you have made. You are also defining Player as game? Please look at my answer if you're confused! nekosiwifi 398 — 3y

Answer this question