I want to fix it so that only the person who bought it can ride it, what should I do

Successfully created a car purchase script. However, there is an error that if one person purchases it, all of them can be purchased together. I want to fix it so that only the person who bought it can ride it, what should I do

game.Players.PlayerAdded:Connect(function(player)

	local ownsCamaro = Instance.new("BoolValue", player)
	ownsCamaro.Name  = "ownsCamaro"
	ownsCamaro.Value = false



	game.ReplicatedStorage.camaro2.OnServerEvent:Connect(function()
		if player.leaderstats.Money.Value >= 60 then
			player.leaderstats.Money.Value -= 60
			player.ownsCamaro.Value = true

		end

	end)

end)

it’s because you are getting the player fromt the function when a player joins try taking out the RemoteEvent function outside of the “PlayerAdded” function and if you want to get the player that is sending the remote event just get it fromt the RemoteEvent something like this

game.Players.PlayerAdded:Connect(function(player)

	local ownsCamaro = Instance.new("BoolValue", player)
	ownsCamaro.Name  = "ownsCamaro"
	ownsCamaro.Value = false

end)


game.ReplicatedStorage.camaro2.OnServerEvent:Connect(function(player)
	if player.leaderstats.Money.Value >= 60 then
		player.leaderstats.Money.Value -= 60
		player.ownsCamaro.Value = true

	end

end)
1 Like

It works perfectly. Thank you Thanks to you, I can sleep ^^

To make sure that only the person who purchased the car can ride it, you need to create a separate BoolValue for each car. In other words, instead of creating one BoolValue called “ownsCamaro” for each player, you need to create a separate BoolValue for each car owned by the player.

game.Players.PlayerAdded:Connect(function(player)

	
	local ownedCars = {}

	game.ReplicatedStorage.camaro2.OnServerEvent:Connect(function()
		if player.leaderstats.Money.Value >= 60 then
			player.leaderstats.Money.Value -= 60

			
			local ownsCamaro = Instance.new("BoolValue", player)
			ownsCamaro.Name = "ownsCamaro"
			ownsCamaro.Value = true

			
			ownedCars[ownsCamaro] = true
		end
	end)

	
	game.ReplicatedStorage.rideCamaro.OnServerInvoke = function()
		for car, value in pairs(ownedCars) do
			if car.Value == true then
				return true
			end
		end
		return false
	end)

end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.