This can be exploited very badly. An exploiter can make their cash to a high number and the LocalScript will see that, but on the server, exploiters can't mess with the server.
Also, your leaderstats should be a Folder instead of an IntValue.
So to set this up, you'd just need to insert a RemoteEvent into ReplicatedStorage.
It's best to insert it into ReplicatedStorage because the client and the server can see the RemoteEvent.
Before we start I highly recommend you to understand RemoteEvents.
LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local coins = leaderstats:WaitForChild("score")
local Backpack = player.Backpack
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
script.Parent.MouseButton1Click:Connect(function()
remoteEvent:FireServer("Ak-47")
end)
I'd put the LocalScript into StarterPlayerScripts. DataModel > StarterPlayer > StarterPlayerScripts
LOCAL SCRIPT EXPLANATION
It's best to use :GetService on services in the explorer if for some reason you changed the name.
So, the first two lines are retrieving the services ReplicatedStorage and Players.
line 4 is getting the player.
line 6 is getting the leaderstats folder.
line 7 is getting the score IntValue.
line 9 is getting the player's Backpack
line 11 is getting the RemoteEvent.
Finally, the function will fire the RemoteEvent to the server using :FireServer with an argument which is the tool's name.
Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local remoteEvent = ReplicatedStorage.RemoteEvent
remoteEvent.OnServerEvent:Connect(function(playerObj, tool)
local coins = playerObj.leaderstats.score
if coins.Value >= 50 then
coins.Value = coins.Value - 50
ServerStorage[tool]:Clone().Parent = playerObj.Backpack
print("Enough")
else
print("Not enough")
end
end)
I'd put the Script into ServerScriptService. DataModel > ServerScriptService
SCRIPT EXPLANATION
So, the first two lines are retrieving the services ReplicatedStorage and ServerStorage.
It's best to have the AK-47 in ServerStorage because the client can't see the contents of ServerStorage.
line 4 is the RemoteEvent. You don't need to use :WaitForChild on the server because everything loads at the same time.
line 6 OnServerEvent will listen out for :FireServer then will trigger the function connected to the event.
line 7 will get the score IntValue.
After that, using a relational operator to compare the player's cash and if true then it'll run the code after the if statement otherwise it will run the code after the else.
line 11 you should use square brackets because tool is an argument passed from the LocalScript.
Hopefully, this works because I just wrote it in here and didn't write the code in Roblox studio.
If you have any problems or you were confused, you can reply to me.
Edit1: Added links and tried to fix some grammar mistakes.
4 comments
Try using "FindFirstChild("leaderstats")" and tell me if it works, if it doesn't, are there any errors?
I got an error it says Players.GrannyCooksBaldGuys.PlayerGui.ScreenGui.Frame.BuySword.LocalScript:14: attempt to index nil with 'Value'
can you check if score and stats are really there when you test it?
Do game.Players.PlayerAdded:Connect(onPlayerEntered) because if you were to add a instance which is not a PlayerObject it will insert a leaderstats folder into the instance.