I'm doing a script wich sets up a function for a remote event. However, my remote event is supposed to subtract cash, and I'm using a server script, so I can't use LocalPlayer to find my player. Here is my script that will use fire the event:
script.Parent.MouseButton1Down:Connect(function()
if game.Workspace.Purchasedbmw.Value == 1 then
-- Spawn it
game.ReplicatedStorage.SpawnCar:FireServer(script.Parent.Parent.Name)
else
-- Let them purchase it
local PriceOfItem = 1000
if game.Players.LocalPlayer.leaderstats.Cash.Value >= PriceOfItem and game.Workspace.Purchasedbmw.Value == 0 then
-- The player has enough cash
game.ReplicatedStorage.RemoteEvent:FireServer() -- This will fire the event (I still don't know what to put in the brackets, I just copied what is on the fifth line)
game.Workspace.Purchasedbmw.Value = 1
print("Value changed successfully!")
script.Parent.Text = "Spawn"
script.Parent.MouseButton1Down:connect(function()
-- Deletes the car.
for i,v in pairs(game.Workspace:GetChildren()) do
if v.Name == game.Players.LocalPlayer.Name.."'s bmw" or v.Name == game.Players.LocalPlayer.Name.."'s mc" or v.Name == game.Players.LocalPlayer.Name.."'s por" or v.Name == game.Players.LocalPlayer.Name.."'s s13" or v.Name == game.Players.LocalPlayer.Name.."'s hoon" or v.Name == game.Players.LocalPlayer.Name.."'s gtr" then
v:Destroy()
end end
end)
end
end
end)
And this is the script that sets up the event function:
game.Players.PlayerAdded:Connect(function(localplayer)
local leaderstats = localplayer:WaitForChild("leaderstats")
local cash = leaderstats:WaitForChild("Cash")
local priceofitem = 1000
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)-- Sets function
cash.Value = cash.Value - priceofitem
end)
end)
I've tried using the playeradded event to define player, but it doesn't seem to be working.

12 comments
I don't think you got it right... you can only fire the event if the value is 1 (it was supposed to be true but I can't do boolvalue datastores so I just use 1 for true and 0 for false). For the value to be 1 (true), you need to BUY the car, so you can't just fire the event at all. I will share my spawn car function script in a moment...
https://gyazo.com/8abca966441ee54102cfa88bf8e42eab
Look what i said at the Top of my answer
and look at the Server Script
I still don't get it... How I will spawn car? Isn't it just better to create another remote event and set the function to your function?
hmm wait lemme edit my code
Ok, let me try now.
i edited a little bit, now it just sets Purchasedbmw to 1 if the player has enough money and Purchasedbmw isn't 1 already
i need a little more info of what you are trying to do. Its about buying cars right? and also why does a Spawn button delete a car?
As I said, 1 is for true and 0 for false, so it is 0. Also, there is something wrong. Look at my second script in my answer. It checks if the value is true (1) and if it is, fires the spawncar event (wich has nothing to do with your event). Else (if the value is not 0) then we let them purchase, so I set up a new remote event with the function u showed me.
It does a loop that checks if there is already a car spawned in, so the player can't just spam the spawn button and spawn a billion cars. So it basically replaces the old car if it exists.
can we continue on Discord insead? LukaGaming#8725