Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to find my currency value in a server script?

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.

2 answers

#Problems

##IMPORTANT This script is Exploitable, Rememeber This ONE rule. Basic Server Side Validation. A clever hacker may just Fire the remote event to Spawn the car, You ALWAYS want to check stuff ON THE SERVER

That being said lets fix this script:

  1. On line 5 you added this Parameter to the FireServer call script.Parent.Parent.Name but the Server never used that Parameter and I don't know what it is.

#Solutions

  1. Check if you have Enough currency ON THE SERVER
  2. I don't think you need extra paramaters to spawn a car (Unless you do, Maybe you want to pass in a specific Model)

#Final Scripts LocalScript:

script.Parent.MouseButton1Down:Connect(function()
	-- Use SERVER-SIDE VALIDATION
	
	 game.ReplicatedStorage.SpawnCar:FireServer(script.Parent.Parent.Name)

          script.Parent.Text = "Spawn"
          script.Parent.MouseButton1Down:connect(function()
          -- Deletes the car.
	  -- Idk what this is. so.....
                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) 

Server Script:

local function SpawnCar(player)
	local leaderstats = player.leaderstats
	local cash = leaderstats.cash
	local priceOfItem = 1000

	if cash.Value >= 1000 and game.Workspace.Purchasedbmw == 0 then -- Checks if player has enough Money, if so Buys the car
		cash.Value = cash.Value - priceOfItem
		game.Workspace.Purchasedbmw.Value = 1
	end
end

game.ReplicatedStorage.SpawnCar.OnServerEvent(SpawnCar)

12 comments

0

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...

0

Look what i said at the Top of my answer

0

and look at the Server Script

0

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?

0

hmm wait lemme edit my code

0

Ok, let me try now.

0

i edited a little bit, now it just sets Purchasedbmw to 1 if the player has enough money and Purchasedbmw isn't 1 already

0

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?

0

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.

0

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.

0

can we continue on Discord insead? LukaGaming#8725

Alright, I figured it out. I initially wanted to change game.Workspace.Purchasedbmw.Value to 1, in a way that the server detects it changed. So I was trying to get the whole script in a remote event, but everything I needed to do is to only make the event change the value. This is the script that defines the function.

game.ReplicatedStorage.ChangeValue.OnServerEvent:Connect(function(player)
	game.Workspace.Purchasedbmw.Value = 1
end)

This is the script that will 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.ChangeValue:FireServer()-- This fires the event that changes value.
			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) 

If you were trying to do the same thing, here is the answer.

1 comment

0

CHECK MY ANSWER ITS IMPORTANT

This discussion is preserved as it was. New answers, comments, and votes are closed.