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

How to make this local script code work on a serverl script?

I'm trying to move this local script code to a server script and I don't know how to find my cash currency since you can't use LocalPlayer on server scripts. Here is my code:

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.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value - PriceOfItem
            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) 

1 comment

0

RemoteEvent is everything you need now.

1 answer

You need to create a RemoteEvent, preferably in ReplicatedStorage. You then fire this remote from the client, with optional arguments if you need any. Now, when handling this remote from the serverside, you'll notice on the wiki that the server gets an extra argument. This means when firing with no arguments, there's still one argument to anticipate on the serverside. This argument is of course, the player who fired it. From there you can do your checks, subtracting money, spawning cars, etc.

1 comment

0

Alright, my car spawner is based on that, hovewer, I got it from a tutorial, so I don't get how it works at all. Do u recommend any youtube tutorials or something like that?

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