So i am working a simulator game and i was working pretty well but then i hit a slump. This script works and all but when a person clicks and earn money the money wont show up for another player.
Here is the script:
local toolName = "AdvancedCard" -- what tool(name) must be held to earn? local randomizeAmount = true -- randomize cash amount? if true, cash will be randomised, if false, cash will be the same everytime local cashStatic = 1 --if 'randomizeAmount' is FALSE, how much money should be earned by 1 click? local cashRandomX = 15 --if 'randomizeAmount' is TRUE, what is the MINimum cash amount? local cashRandomY = 20 --if 'randomizeAmount' is TRUE, what is the MAXimum cash amount? local debugging = true --debugging mode? if true, info will be print to the console, if false, script is not printing anything local leaderstatName = "Cash" --name of Cash leaderstat ---------- local plr = game.Players.LocalPlayer local equip = false local mouse = plr:GetMouse() plr.Backpack:WaitForChild(toolName).Equipped:Connect(function() equip = true end) plr.Backpack:WaitForChild(toolName).Unequipped:Connect(function() equip = false end) mouse.Button1Down:Connect(function() if equip then wait(0.5) if randomizeAmount then local amount = math.random(cashRandomX, cashRandomY) plr.leaderstats:WaitForChild(leaderstatName).Value = plr.leaderstats:WaitForChild(leaderstatName).Value + amount if debugging then print("Player "..plr.Name.." just earned $"..amount.." for clicking with the tool.") end else plr.leaderstats:WaitForChild(leaderstatName).Value = plr.leaderstats:WaitForChild(leaderstatName).Value + cashStatic if debugging then print("Player "..plr.Name.." just earned $"..cashStatic.." for clicking with the tool.") end end end end)
Please help me fix this.
You can't change a value on the server from a local script on filtering enabled games because it won't replicate to other clients. Filtering will make sure this change is not replicated. You should use a remote event or a remote function to do this, preferably a remote event.
You should try something like this:
(Please note, this is very easy to exploit, apply some good security to your remotes.)
Server:
-- Script (on server) -- Create a remote event that will be fired to update player's cash. local RE = Instance.new("RemoteEvent", game.ReplicatedStorage)) RE.Name = "UpdateCash" RE.OnServerEvent:Connect(function(self, value) self.leaderstats.Cash.Value = self.leaderstats.Cash.Value + value end)
Client:
-- Local Script (on client) repeat wait() until game:GetService("Players").LocalPlayer local lp = game.Players.LocalPlayer local lpM = lp:GetMouse() local randomizeAmount = true lp.Backpack:WaitForChild("AdvancedCard").Equipped:Connect(function() local debounce = false lpM.MouseButton1Down:Connect(function() if debounce == true then wait(0.5) debounce = not debounce else if randomizeAmount == true then game.ReplicatedStorage.UpdateCash:FireServer(math.random(15, 20)) else game.ReplicatedStorage.UpdateCash:FireServer(1) end debounce = not debounce end end) end)
Sorry if I left behind any errors or bad coding habits, un-testing code and kind of rushing.