Jump to content

Anyway to allow non-admin players to spawn vehicle?


Eroge

Recommended Posts

Im using this script to allow players purchase vehicles with keys. But I got 3 problems:

1. Car key does not spawn correctly

2. Only players with GM or higher access can spawn vehicle, others only get "you dont have the right to use /addvehicle command" in chatbox

3. Spawned cars are not repaired correctly

 

Can anyone tell me where did this script go wrong? Much appreciated!!

vehicleTradeRecipe.lua

Link to comment
Share on other sites

I think you can resolve all three with client-server commands, using functions sendClientCommand, sendServerCommand, and events OnClientCommand, OnServerCommand. There are plenty of examples in the code! For example, this built-in command should repair vehicles properly (see VehicleCommands.lua):

sendClientCommand(player, "vehicle", "repair", { vehicle = vehicle:getId() })
Edited by Tykvesh
Link to comment
Share on other sites

21 hours ago, Tykvesh said:

I think you can resolve all three with client-server commands, using functions sendClientCommand, sendServerCommand, and events OnClientCommand, OnServerCommand. There are plenty of examples in the code! For example, this built-in command should repair vehicles properly (see VehicleCommands.lua):

sendClientCommand(player, "vehicle", "repair", { vehicle = vehicle:getId() })

Any examples for spawning vehicles?

Link to comment
Share on other sites

Here's an example for your script:

function Recipe.OnCreate.hezhuangqichea(items, result, player)
	if getWorld():getGameMode() ~= "Multiplayer" then
	for i=0,items:size() - 1 do
		local item = items:get(i)
		if item:getType() == "carboxmustangpolice" then
			if not player:isOutside() or player:getZ() > 0 then
				player:Say(getText("IGUI_zuzhuangshibai"))
				player:getInventory():AddItem("carboxmustangpolice")
			else
				--first we tell the server to spawn a vehicle for us
				sendClientCommand(player, "eroge", "SpawnVehicle", { type = "Base.ATAMustangPolice" })
			end
Events.OnClientCommand.Add(function(module, name, player, params)
    if module == "eroge" and name == "SpawnVehicle" then
		--server got the request and spawns the desired vehicle
		local vehicle = addVehicleDebug(params.type, randomdir[ZombRand(4) + 1], -1, getSquare(player:getX(), player:getY(), player:getZ()))
			
		--should repair properly as this code is run server-side
		vehicle:repair()
		
		--tell back to the player to do their client-side stuff
		sendServerCommand(player, "eroge", "ClaimVehicle", { vehicle = vehicle:getId() })		
	end
end)
Events.OnServerCommand.Add(function(module, name, params)
	if module == "eroge" and name == "ClaimVehicle" then
		local vehicle = getVehicleById(params.vehicle)
		if vehicle ~= nil then
			--now client can get some keys in peace
			player:getInventory():AddItem(vehicle:getCurrentKey() or vehicle:createVehicleKey())
			player:Say(getText("IGUI_zuzhuangchenggong"))
		end
	end
end)
Edited by Tykvesh
Link to comment
Share on other sites

On 2/5/2022 at 2:13 AM, Tykvesh said:

Here's an example for your script:

function Recipe.OnCreate.hezhuangqichea(items, result, player)
	if getWorld():getGameMode() ~= "Multiplayer" then
	for i=0,items:size() - 1 do
		local item = items:get(i)
		if item:getType() == "carboxmustangpolice" then
			if not player:isOutside() or player:getZ() > 0 then
				player:Say(getText("IGUI_zuzhuangshibai"))
				player:getInventory():AddItem("carboxmustangpolice")
			else
				--first we tell the server to spawn a vehicle for us
				sendClientCommand(player, "eroge", "SpawnVehicle", { type = "Base.ATAMustangPolice" })
			end
Events.OnClientCommand.Add(function(module, name, player, params)
    if module == "eroge" and name == "SpawnVehicle" then
		--server got the request and spawns the desired vehicle
		local vehicle = addVehicleDebug(params.type, randomdir[ZombRand(4) + 1], -1, getSquare(player:getX(), player:getY(), player:getZ()))
			
		--should repair properly as this code is run server-side
		vehicle:repair()
		
		--tell back to the player to do their client-side stuff
		sendServerCommand(player, "eroge", "ClaimVehicle", { vehicle = vehicle:getId() })		
	end
end)
Events.OnServerCommand.Add(function(module, name, params)
	if module == "eroge" and name == "ClaimVehicle" then
		local vehicle = getVehicleById(params.vehicle)
		if vehicle ~= nil then
			--now client can get some keys in peace
			player:getInventory():AddItem(vehicle:getCurrentKey() or vehicle:createVehicleKey())
			player:Say(getText("IGUI_zuzhuangchenggong"))
		end
	end
end)

Sorry but what does module == "eroge" means? Im totally noob to scripting

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...