So I made a shop where you sell coconuts for cat food and xp, and the script completely broke and changed the values to all of the players in the server, so I made a new script and it does not work.
The script is a server script inside of startergui.
Here’s the script:
--Server script--
game.ReplicatedStorage.Events.Sold.SoldCoconuts.OnServerEvent:Connect(function(Player)
print("2")
local PlayerGui = Player:WaitForChild("PlayerGui")
local leaderstats = Player:WaitForChild("leaderstats")
local Coconuts = leaderstats:WaitForChild("Coconuts")
local Catfood = leaderstats:WaitForChild("CatFood")
local XP = leaderstats:WaitForChild("XP")
if PlayerGui.Zone3GUI.SellCoconuts.SellFrame.OneCoconutForFiveCatfood.IsSelected.Value == true then
print("3")
if Coconuts.Value >= 1 then
print("4")
Coconuts.Value = Coconuts.Value - 1
Catfood.Value = Catfood.Value + 5
end
end
if PlayerGui.Zone3GUI.SellCoconuts.SellFrame.OneCoconutForThreeHundredXP.IsSelected.Value == true then
if Coconuts.Value >= 1 then
Coconuts.Value = Coconuts.Value - 1
XP.Value = XP.Value + 300
end
end
if PlayerGui.Zone3GUI.SellCoconuts.SellFrame.TenCoconutsForFiftyCatfood.IsSelected.Value == true then
if Coconuts.Value >= 10 then
Coconuts.Value = Coconuts.Value - 10
Catfood.Value = Catfood.Value + 50
end
end
if PlayerGui.Zone3GUI.SellCoconuts.SellFrame.TenCoconutsForThreeThousandXP.IsSelected.Value == true then
if Coconuts.Value >= 10 then
Coconuts.Value = Coconuts.Value - 10
XP.Value = XP.Value + 3000
end
end
end)
Why not use a LocalScript instead to reference the LocalPlayer that way? Instead of having to reference it by using so much script.Parent’s, you could just use a RemoteEvent to handle client/server transportation by sending a request from the client to the server that way, plus you can reference the player who fired the Event via using OnServerEvent’s Event
Hence why I said to use a RemoteEvent, to handle that request from the client to the server
You can fire the event from the client, and be able to receive that said request on the server to only change that specific player’s values
If you change it from the client-side only, it’ll only happen for you and not for the server which is why you have to change it from the server-side
Your script seems to work, but like you said it changes the values for all players which is why you should use a LocalScript to account for every individual player in the game
Here’s what I would do:
Create a RemoteEvent inside the ReplicatedStorage Service, as that can handle client/server replication
Make sure to call FireServer() on the client, and OnServerEvent on the server to properly handle whenever the events get called
Handle your sanity checks on the server for exploit-prevention
--Local Script
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
script.Parent.MouseButton1Click:Connect(function()
Event:FireServer()
end)
--Server Script inside ServerScriptService
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
Event.OnServerEvent:Connect(function(Player) --The player is automatically passed in as the first parameter
local PlayerGui = Player:WaitForChild("PlayerGui")
local leaderstats = Player:WaitForChild("leaderstats")
local Coconuts = leaderstats:WaitForChild("Coconuts")
local Catfood = leaderstats:WaitForChild("CatFood")
local XP = leaderstats:WaitForChild("XP")
--Now, I don't know your Gui hierarchy here so you have to reference your UI Values from there via using the PlayerGui variable
if OneCoconutForFiveCatfood.IsSelected.Value == true then
if Coconuts.Value >= 1 then
Coconuts.Value = Coconuts.Value - 1
Catfood.Value = Catfood.Value + 5
end
end
if OneCoconutForThreeHundredXP.IsSelected.Value == true then
if Coconuts.Value >= 1 then
Coconuts.Value = Coconuts.Value - 1
XP.Value = XP.Value + 300
end
end
if TenCoconutsForFiftyCatfood.IsSelected.Value == true then
if Coconuts.Value >= 10 then
Coconuts.Value = Coconuts.Value - 10
Catfood.Value = Catfood.Value + 50
end
end
if TenCoconutsForThreeThousandXP.IsSelected.Value == true then
if Coconuts.Value >= 10 then
Coconuts.Value = Coconuts.Value - 10
XP.Value = XP.Value + 3000
end
end
end)
It might also be the fact that you’re checking for the Values inside the Gui, when personally I would put them inside the Player Object instead
local rp_s1 = game:GetService(“ReplicatedStorage”)
local pl_s1 = game:GetService(“Players”)
local a1 = rp_s1:WaitForChild(“IDK”)
local a2 = pl_s1.LocalPlayer
local a3 = script.Parent
local function IDK()
a1:FireServer(a2)
print(“LOl :3”)
end
a3.ScreenGui.TextButton.MouseButton1Click:Connect(IDK)
Server Script
local rp_s1 = game:GetService(“ReplicatedStorage”)
local a1 = rp_s1:WaitForChild(“IDK”)
local function Check_Remote(tp_t1)
if tp_t1:IsA(“Player”) then
print(“BRUHHHHHH”)
if tp_t1:FindFirstChild(“leaderstats”) then
local a1 = tp_t1:WaitForChild(“leaderstats”)
local a2 = a1:WaitForChild(“Gold”)
a2.Value = a2.Value + 1
end
else
print(“Dont is a player :C”)
end
end
a1.OnServerEvent:Connect(Check_Remote)
Since I have 4 remote events, 4 buttons and 1 sell button I did something like this, but it didn’t work:
Local Script
local leaderstats = script.Parent.Parent.Parent.Parent.Parent.Parent.leaderstats
local Coconuts = leaderstats:WaitForChild("Coconuts")
local Catfood = leaderstats:WaitForChild("CatFood")
local XP = leaderstats:WaitForChild("XP")
local OneCoconutForFiveCatfood = script.Parent.Parent.Parent.SellFrame.OneCoconutForFiveCatfood
local OneCoconutForThreeHundredXP = script.Parent.Parent.Parent.SellFrame.OneCoconutForThreeHundredXP
local TenCoconutsForFiftyCatfood = script.Parent.Parent.Parent.SellFrame.TenCoconutsForFiftyCatfood
local TenCoconutsForThreeThousandXP = script.Parent.Parent.Parent.SellFrame.TenCoconutsForThreeThousandXP
script.Parent.MouseButton1Click:Connect(function()
if OneCoconutForFiveCatfood.IsSelected.Value == true then
if Coconuts.Value >= 1 then
game.ReplicatedStorage.Events.SoldCoconuts.SoldOneCoconutForFiveCatfood:FireServer()
end
end
if OneCoconutForThreeHundredXP.IsSelected.Value == true then
if Coconuts.Value >= 1 then
game.ReplicatedStorage.Events.SoldCoconuts.SoldOneCoconutForThreeHundredXP:FireServer()
end
end
if TenCoconutsForFiftyCatfood.IsSelected.Value == true then
if Coconuts.Value >= 10 then
game.ReplicatedStorage.Events.SoldCoconuts.SoldTenCoconutsForFiftyCatfood:FireServer()
end
end
if TenCoconutsForThreeThousandXP.IsSelected.Value == true then
if Coconuts.Value >= 10 then
game.ReplicatedStorage.Events.SoldCoconuts.SoldTenCoconutsForThreeThousandXP:FireServer()
end
end
end)
Server Script
game.ReplicatedStorage.Events.SoldCoconuts.SoldOneCoconutForFiveCatfood.OnServerEvent:Connect(function(Player)
local PlayerGui = Player:WaitForChild("PlayerGui")
local leaderstats = Player:WaitForChild("leaderstats")
local Coconuts = leaderstats:WaitForChild("Coconuts")
local Catfood = leaderstats:WaitForChild("CatFood")
local XP = leaderstats:WaitForChild("XP")
if PlayerGui.Zone3GUI.SellCoconuts.SellFrame.OneCoconutForFiveCatfood.IsSelected.Value == true then
if Coconuts.Value >= 1 then
Coconuts.Value = Coconuts.Value - 1
Catfood.Value = Catfood.Value + 5
end
end
if PlayerGui.Zone3GUI.SellCoconuts.SellFrame.OneCoconutForThreeHundredXP.IsSelected.Value == true then
if Coconuts.Value >= 1 then
Coconuts.Value = Coconuts.Value - 1
XP.Value = XP.Value + 300
end
end
if PlayerGui.Zone3GUI.SellCoconuts.SellFrame.TenCoconutsForFiftyCatfood.IsSelected.Value == true then
if Coconuts.Value >= 10 then
Coconuts.Value = Coconuts.Value - 10
Catfood.Value = Catfood.Value + 50
end
end
if PlayerGui.Zone3GUI.SellCoconuts.SellFrame.TenCoconutsForThreeThousandXP.IsSelected.Value == true then
if Coconuts.Value >= 10 then
Coconuts.Value = Coconuts.Value - 10
XP.Value = XP.Value + 3000
end
end
end)
Don’t use 4 RemoteEvents…Use only 1 as I provided earlier in the script
It’ll result as a complete mess if you use multiple RemoteEvents, when you could just properly handle the sanity checks on the server with no issue at all
game.ReplicatedStorage.Events.Sold.SoldCoconuts.OnServerEvent:Connect(function(Player)
local PlayerGui = Player:WaitForChild("PlayerGui")
local leaderstats = Player:WaitForChild("leaderstats")
local Coconuts = leaderstats:WaitForChild("Coconuts")
local Catfood = leaderstats:WaitForChild("CatFood")
local XP = leaderstats:WaitForChild("XP")
if PlayerGui.Zone3GUI.SellCoconuts.SellFrame.OneCoconutForFiveCatfood.IsSelected.Value == true then
if Coconuts.Value >= 1 then
Coconuts.Value = Coconuts.Value - 1
Catfood.Value = Catfood.Value + 5
end
end
if PlayerGui.Zone3GUI.SellCoconuts.SellFrame.OneCoconutForThreeHundredXP.IsSelected.Value == true then
if Coconuts.Value >= 1 then
Coconuts.Value = Coconuts.Value - 1
XP.Value = XP.Value + 300
end
end
if PlayerGui.Zone3GUI.SellCoconuts.SellFrame.TenCoconutsForFiftyCatfood.IsSelected.Value == true then
if Coconuts.Value >= 10 then
Coconuts.Value = Coconuts.Value - 10
Catfood.Value = Catfood.Value + 50
end
end
if PlayerGui.Zone3GUI.SellCoconuts.SellFrame.TenCoconutsForThreeThousandXP.IsSelected.Value == true then
if Coconuts.Value >= 10 then
Coconuts.Value = Coconuts.Value - 10
XP.Value = XP.Value + 3000
end
end
end)
What didn’t work exactly? I’m pretty sure that the client side should’ve worked, but it could be something to do with the server side perhaps
The only potential issue I could see is that you’re checking for the Value inside the PlayerGui itself, where personally I’d put it inside the Player Object alone for it to detect
I’d just recommend debugging & adding print() statements to check what specifically doesn’t work
Are there any errors at all in your Output? Again, I’d just put your Value Objects inside the Player Object, rather than placing them inside the PlayerGui itself