Extremo Posted June 13, 2012 Share Posted June 13, 2012 (edited) Hey guys, It seems I've run into another problem but it isn't clear to me what exactly is wrong. So I got the following code: Serverside: function createPedForPlayer(thePlayer, command, pedModel) local x,y,z = getElementPosition(thePlayer) local ped = createPed( tonumber(pedModel), x, y, z, 0.0, true) if ped == nil then outputChatBox("Error ped was not created.", thePlayer) return end triggerClientEvent("OnPedWalk", getRootElement(), thePlayer, ped) end addCommandHandler("createPed", createPedForPlayer) Clientside: local ped = {} function makeWalk(thePlayer, thePed) ped[thePlayer] = thePed setPedControlState(thePed, "walk", true) end addEvent("OnPedWalk", true) addEventHandler("OnPedWalk", getRootElement(), makeWalk) I don't see what exactly may be wrong here? Could someone point me into the right direction? That would be very appreciated =). EDIT: Forgot to mention what is actually happening. Which is nothing. The ped is not starting to walk and that is what makes me wonder here. Edited June 13, 2012 by Guest Link to comment
myonlake Posted June 13, 2012 Share Posted June 13, 2012 Try this. Server-side addCommandHandler("createped", function(player, cmd, model) local model = tonumber(model) if model then local x, y, z = getElementPosition(player) local rotation = getPedRotation(player) local ped = createPed(model, x, y, z, rotation, true) triggerClientEvent("onPedWalk", root, ped) else outputChatBox("Error: Ped was not created due to invalid model.", player, 255, 0, 0, false) end end ) Client-side local peds = {} addEvent("onPedWalk", true) addEventHandler("onPedWalk", root, function(ped) table.insert(peds, ped) setPedControlState(ped, "walk", true) end ) Link to comment
Extremo Posted June 13, 2012 Author Share Posted June 13, 2012 Hey, Apart from the table.insert I don't see much difference. Also, it actually doesn't save the ped's related to the player but only stores the peds related to peds which is not what I want. I want the ped to store in that array relative to the player's index. I hope you get my point. However since yours isnt really that different how'd yours make a difference? The ped is being created fine however it doesn't start moving. Link to comment
Guest Guest4401 Posted June 13, 2012 Share Posted June 13, 2012 (edited) EDIT: Forgot to mention what is actually happening. Which is nothing. The ped is not starting to walk and that is what makes me wonder here. I think you must use setPedAnimation. setPedAnimation(ped,"ped","walk_player") Edited June 13, 2012 by Guest4401 Link to comment
Extremo Posted June 13, 2012 Author Share Posted June 13, 2012 EDIT: Forgot to mention what is actually happening. Which is nothing. The ped is not starting to walk and that is what makes me wonder here. I think you must use setPedAnimation. Well that would just play the animation but not move the ped. Link to comment
DiSaMe Posted June 13, 2012 Share Posted June 13, 2012 Your code seems to be correct, and I see the reason why the ped doesn't move. You need to set "forwards" control state to true because "walk" only changes the moving speed from running to walking. It has no effect unless the ped moves. Link to comment
Extremo Posted June 13, 2012 Author Share Posted June 13, 2012 Your code seems to be correct, and I see the reason why the ped doesn't move. You need to set "forwards" control state to true because "walk" only changes the moving speed from running to walking. It has no effect unless the ped moves. I am afraid but that didn't fix the issue. Link to comment
DiSaMe Posted June 13, 2012 Share Posted June 13, 2012 (edited) Maybe try debugging your code? Like adding outputChatBox into makeWalk to see if the function gets called at all? Edit: I found one more possible reason: On the moment the element is created, it's not streamed in for the player. setPedControlState probably doesn't work on peds which are streamed out. You probably need to set control state in onCilentElementStreamIn. Edited June 13, 2012 by Guest Link to comment
Extremo Posted June 13, 2012 Author Share Posted June 13, 2012 Maybe try debugging your code? Like adding outputChatBox into makeWalk to see if the function gets called at all? Already did. It does and I also checked if any of them is nil - they aren't. In fact thePlayer is correct. Not sure if thePed arguement is passed on correctly though. Link to comment
Guest Guest4401 Posted June 13, 2012 Share Posted June 13, 2012 (edited) --server addCommandHandler("createped", function(player, cmd, model) local model = tonumber(model) if model then local x, y, z = getElementPosition(player) local rotation = getPedRotation(player) local ped = createPed(model, x, y, z, rotation, true) triggerClientEvent('onPedWalk', root, ped) else outputChatBox("Error: Ped was not created due to invalid model.", player, 255, 0, 0, false) end end ) --client local peds = {} addEvent("onPedWalk", true) addEventHandler("onPedWalk", root, function(ped) table.insert(peds, ped) setPedAnimation(ped,"ped","walk_player") end ) Edited June 13, 2012 by Guest4401 Link to comment
X-SHADOW Posted June 13, 2012 Share Posted June 13, 2012 --serverSide addCommandHandler("createped", function(player, cmd, model) local model = tonumber(model) if model then local x, y, z = getElementPosition(player) local rotation = getPedRotation(player) local ped = createPed(model, x, y, z, rotation, true) triggerClientEvent('onPedWalk', root, ped) else outputChatBox("Error: Ped was not created due to invalid model.", player, 255, 0, 0, false) end end ) ---clientSide local peds = {} addEvent("onPedWalk", true) addEventHandler("onPedWalk", root, function(ped) table.insert(peds, ped) setPedControlState(ped, 'walk', true) setPedControlState(ped, 'forwards', true) end) Link to comment
DiSaMe Posted June 13, 2012 Share Posted June 13, 2012 Serverside (same as your original code): function createPedForPlayer(thePlayer, command, pedModel) local x,y,z = getElementPosition(thePlayer) local ped = createPed( tonumber(pedModel), x, y, z, 0.0, true) if ped == nil then outputChatBox("Error ped was not created.", thePlayer) return end triggerClientEvent("OnPedWalk", getRootElement(), thePlayer, ped) end addCommandHandler("createPed", createPedForPlayer) Clientside: local ped = {} function makeWalk(thePlayer, thePed) ped[thePlayer] = thePed addEventHandler("onClientElementStreamIn", thePed, makeWalkOnStreamIn) end addEvent("OnPedWalk", true) addEventHandler("OnPedWalk", getRootElement(), makeWalk) function makeWalkOnStreamIn() setPedControlState(source, "walk", true) setPedControlState(source, "forwards", true) removeEventHandler("onClientElementStreamIn", source, makeWalkOnStreamIn) end Because makeWalk is called when the ped is created, it's possible that the ped hasn't streamed in for player and setPedControlState doesn't work. So instead, it should wait until the ped is streamed in (onClientElementStreamIn gets called) and then set control state. Link to comment
Extremo Posted June 13, 2012 Author Share Posted June 13, 2012 Right, I sort of fixed the issue. Apparently the issue was using "table.insert" which I used after I saw it being used by one of the above posters. Though now that it does work, another code that was connected to it still doesn't. Could someone have a look at this? I am sorry I don't mean to ask you all to code for me but I am not so familiar with this all. The solution to my first problem was just adding forward btw. The problem however why it didn't work even though I added forward was because the lua script crashed at "table.insert". After I reverted that change for obvious reasons it worked flawlessly. So I don't know if it was just me or table.insert wasn't the best solution at hand. Either way, this is my new problem: Clientside - below the makeWalk part function findRotation(x1,y1,x2,y2) local t = -math.deg(math.atan2(x2-x1,y2-y1)) if t < 0 then t = t + 360 end; return t; end function UpdatePeds() local players = getElementsByType( "player" ) for k, p in ipairs(players) do if not ped[p] == nil then local x,y,z = getElementPosition(p) local px,py,pz = getElementPosition(ped[p]) local a = findRotation(px,py,x,y) setPedRotation(ped[p], a) end end end setTimer(UpdatePeds, 500, 5) It doesn't seem to update the rotation at all. Does anyone have a clue why? Any help is appreciated. Sorry if I am bothering anyone here. Link to comment
DiSaMe Posted June 13, 2012 Share Posted June 13, 2012 You make setTimer call the function 5 times every 0.5 seconds. That means the function won't be called after 2.5 seconds have passed. Link to comment
Extremo Posted June 13, 2012 Author Share Posted June 13, 2012 You make setTimer call the function 5 times every 0.5 seconds. That means the function won't be called after 2.5 seconds have passed. Oh, I thought that was the time the function may have to execute. Ah I just read the wiki again and I clearly misread it. I thought it said: "TimeToExecuteFunction" Thanks again! EDIT: It doesn't seem to work even after changing the SetTimer thing. Link to comment
X-SHADOW Posted June 13, 2012 Share Posted June 13, 2012 why using this 2 times ? local x,y,z = getElementPosition(p) local px,py,pz = getElementPosition(ped[p]) Link to comment
Extremo Posted June 13, 2012 Author Share Posted June 13, 2012 why using this 2 times ?local x,y,z = getElementPosition(p) local px,py,pz = getElementPosition(ped[p]) Because one gets the position of the player, the other however gets the position of the ped? Link to comment
DiSaMe Posted June 13, 2012 Share Posted June 13, 2012 The problem is in this line: if not ped[p] == nil then Because "not" has higher priority than "==", this line checks if (not ped[p]) is equal to nil. (not ped[p]) can only be true or false, so it will never be equal to nil. Either use: if not (ped[p] == nil) then or simply: if ped[p] then Link to comment
X-SHADOW Posted June 13, 2012 Share Posted June 13, 2012 Try this, function findRotation(x1,y1,x2,y2) local t = -math.deg(math.atan2(x2-x1,y2-y1)) if t < 0 then t = t + 360 end return t end function updatePeds () local players = getElementsByType("player") for k, p in ipairs(players) do if not (ped[p] == nil) then local x,y = getElementPosition(p) local px,py = getElementPosition(ped[p]) local a = findRotation(px,py,x,y) setPedRotation(ped[p], a ) end end end setTimer(updatePeds, 500, 5) Link to comment
Extremo Posted June 13, 2012 Author Share Posted June 13, 2012 Thanks for figuring out that one but it didn't fix the whole thing either way. It still doesn't work so I added a debug to see if it even finds any "ped[p]" that isn't nil and it doesn't. Which leads me to the question that maybe the "ped" array is wrong? EDIT: I forgot to mention tho that I use the "ped" array already before-hand to make the ped walk forwards and that works fine. However inside the loop the ped array doesn't seem to work properly. Can you see any reason why? Link to comment
Extremo Posted June 13, 2012 Author Share Posted June 13, 2012 Try mine . You only made some code to make the ped walk forwards and I've already made that - my concern lies within the function to update the rotation for every player which I posted here: Click me EDIT: Nevermind I just saw yours lol. Must of skipped that one somehow. EDIT2: Yeah I already changed the if like you have, so the problem is probably not in there. You also have the SetTimer thing execute the UpdatePeds only 5 times and I've already removed that as well. The "if" looks like this: if not (ped[p]==nil) then So the problem isn't in there. It's rather that for some weird reason, ped[p] is always nil. Link to comment
X-SHADOW Posted June 13, 2012 Share Posted June 13, 2012 So mine work or not if not show /debugscript 3 Link to comment
Extremo Posted June 13, 2012 Author Share Posted June 13, 2012 So mine work or not if not show /debugscript 3 Thanks I found the problem. I left out the third arguement on SetTimer assuming that it would automatically be zero, but instead it actually didn't create the timer at all. Silly me. This is now solved. Thanks for all the help =) Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now