Jump to content

[Help] Player Pick up Objects Error


Ice_Cool

Recommended Posts

Hello everyone. So i am trying to make a script where a player can create boxes (under a marker) that will be attached to his bones and he (player) can put those boxes (object) on ground.  Now the problem is, if he creates more than 1 boxes (objects) and drops them on floor, he can only pick the very recent one that he created. 

I am trying to make the player be able to pick any object with that ID from the ground either he created or not. But in this script he only picks the very recent he created. 

a = createMarker(-705, 963, 13, "cylinder", 2, 255)
function getTheBox(player, command)
	x, y, z = getElementPosition(player)
	if isElementWithinMarker(player, a) then
		if  exports.bone_attach:isElementAttachedToBone (box) == false then
			box = createObject(1271, x, y, z)
			var = exports.bone_attach:attachElementToBone (box, player, 11, -0.15, 0.35, 0.35, 0, 0, 0)
			anim = setPedAnimation(player,"CARRY","crry_prtial",0,true,true,false,true) == true
		else
			outputChatBox('You already got the box')
		end
	end
end
addCommandHandler('buy', getTheBox)
function PutBoxDown(player, command)
	b, c, d = getElementPosition(player)
	if var == true then
		exports.bone_attach:detachElementFromBone (box)
		if anim == true then
			anim = setPedAnimation(player,"CARRY","put_dwn", 0, true,true,false,true)
			moveObject(box, 0, b, c+0.6, d-0.6)
		end
	else
		outputChatBox('no')
	end
end
addCommandHandler('putdown', PutBoxDown)
function PickUpBox(player)
	if isElement(box) == true then
		playerA, playerB, playerC = getElementPosition(player)
		boxA, boxB, boxC = getElementPosition(box)
		if getDistanceBetweenPoints3D (playerA, playerB, playerC, boxA, boxB, boxC) <= 2 then
			if var == true then
				exports.bone_attach:attachElementToBone (box, player, 11, -0.15, 0.35, 0.35, 0, 0, 0)
			else
				outputChatBox('You already holding the box.')
			end
		else
			outputChatBox('Nothing Around')
		end
	else
		outputChatBox('Nothing Around')
	end
end
addCommandHandler('try', PickUpBox)

 I would be highly thankful if someone enlighten me with the best suggestions to fix this bug. Thank you.

Link to comment

Someone please reply? 

Re-posting above Code (Sorry i mistakenly added above code in snippet) 

a = createMarker(-705, 963, 13, "cylinder", 2, 255)
function getTheBox(player, command)
	x, y, z = getElementPosition(player)
	if isElementWithinMarker(player, a) then
		if  exports.bone_attach:isElementAttachedToBone (box) == false then
			box = createObject(1271, x, y, z)
			var = exports.bone_attach:attachElementToBone (box, player, 11, -0.15, 0.35, 0.35, 0, 0, 0)
			anim = setPedAnimation(player,"CARRY","crry_prtial",0,true,true,false,true) == true
		else
			outputChatBox('You already got the box')
		end
	end
end
addCommandHandler('buy', getTheBox)
function PutBoxDown(player, command)
	b, c, d = getElementPosition(player)
	if var == true then
		exports.bone_attach:detachElementFromBone (box)
		if anim == true then
			anim = setPedAnimation(player,"CARRY","put_dwn", 0, true,true,false,true)
			moveObject(box, 0, b, c+0.6, d-0.6)
		end
	else
		outputChatBox('no')
	end
end
addCommandHandler('putdown', PutBoxDown)
function PickUpBox(player)
	if isElement(box) == true then
		playerA, playerB, playerC = getElementPosition(player)
		boxA, boxB, boxC = getElementPosition(box)
		if getDistanceBetweenPoints3D (playerA, playerB, playerC, boxA, boxB, boxC) <= 2 then
			if var == true then
				exports.bone_attach:attachElementToBone (box, player, 11, -0.15, 0.35, 0.35, 0, 0, 0)
			else
				outputChatBox('You already holding the box.')
			end
		else
			outputChatBox('Nothing Around')
		end
	else
		outputChatBox('Nothing Around')
	end
end
addCommandHandler('try', PickUpBox)

 

Edited by Ice_Cool
Link to comment
  • Moderators
4 hours ago, Ice_Cool said:

Re-posting above Code

Use a table, so that you can separate player data. And if you do not know how to work with a table, then go watch table tutorials on Youtube.

 

local allPlayerData = {}

addEventHandler("onPlayerJoin", root, function () 
  allPlayerData[source] = {} -- initial state
end)

addEventHandler("onPlayerQuit", root, function () 
  allPlayerData[source] = nil
end, true, "low")

 

local box = createObject(1271, x, y, z)

allPlayerData[player]["box"] = box -- save

local box = allPlayerData[player]["box"] -- load

 

Edited by IIYAMA
Link to comment

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...