louie999 Posted October 5, 2015 Share Posted October 5, 2015 So I've been banging my head over this problem for awhile now, I can't seem to fix it. I've created a simple turret system but I can't seem to fix it's problem, it supposed to shoot anyone that comes inside its attack range(a ColSphere), it attacks me when I come inside the ColSphere but the problem is it doesn't do any damage(the shots), the only time the shots will ever do any damage is when I get out of the ColSphere. It's really better explained in a video. So what happens here basically is: I go inside the turret's range, it attacks me, rotates correctly, but does no damage. Then when I go out of its range then the shots do damage, doesn't rotate anymore, and basically just sits there shooting. Anyone can help me with this problem? (I will show codes if needed...) Thanks in advance... Link to comment
Moderators Citizen Posted October 6, 2015 Moderators Share Posted October 6, 2015 Of course we need the code to understand what's wrong (you can cut some parts if it's not related to the problem). For the explosions not doing any damage inside the colshape, I think there is, somewhere in the code, a code to cancel any damage inside the colshape like a safe-zone or something like that. Look for the use of onClientPlayerDamage in all the resources currently running with your code and see where that event is canceled and the conditions (if statements) for it. For the second part of the problem, you should kill the timer that is making the turret to shoot (because I guess you started an infinite timer when you entered in the colshape) if there is no player in the colshape anymore. Link to comment
Buffalo Posted October 6, 2015 Share Posted October 6, 2015 There are numerous problems with ped using weapons, as I was trying to use them in early 2013, I don't think its fixed. I would suggest you using simple createWeapon instead of a ped with a weapon, it won't change your concept and also should work as you intended. Link to comment
louie999 Posted October 7, 2015 Author Share Posted October 7, 2015 @Citizen, I don't think my safezone script is causing problems, I've tried my turret system with the safezone script turned off and it still gave me the same results. As for the scripts, I've changed it, I had a look at custom weapons(like Buffalo said) and it's also good , though I'm having a problem with this script tho, the turret(custom weapon) spawns fine but it does nothing, it was supposed to shoot when I go inside its colSphere, I've received errors also: [2015-10-07 17:10:49] WARNING: Turret\Turret.lua:47: Bad argument @ 'getElementData' [Expected element at argument 1, got boolean] [2015-10-07 17:10:49] WARNING: Turret\Turret.lua:48: Bad argument @ 'getElementData' [Expected element at argument 1, got boolean] [2015-10-07 17:10:50] WARNING: Turret\Turret.lua:33: Bad argument @ 'getElementData' [Expected element at argument 1, got boolean] [2015-10-07 17:10:50] WARNING: Turret\Turret.lua:35: Bad argument @ 'setElementData' [Expected element at argument 1, got boolean] [2015-10-07 17:10:50] WARNING: Turret\Turret.lua:36: Bad argument @ 'triggerClientEvent' [Expected element at argument 3, got boolean] Here's my turret code: Turret.lua -- Server Functions function spawnTurret(modelid, x, y, z, weaponType, isProj, range, xOffset, yOffset, zOffset) if (not x) or (not y) or (not z) or (not weaponType) then error("Missing arguments, turret spawning failed") end if not isProj then isProj = false end local range_col = createColSphere(x, y, z, range) local base = createObject(modelid, x, y, z) setElementData(range_col, "turret_part", true) triggerClientEvent(getRootElement(), "onTurretCreate", getRootElement(), x, y, z, base, range_col, weaponType, isProj, range, xOffset, yOffset, zOffset) end addEvent("customizeNewTurret", true) function SetTurretData(turret, base, range_col, weaponType, isProj, range, xOffset, yOffset, zOffset) setElementData(range_col, "parent", turret) setElementData(turret, "turret", true) setElementData(turret, "base", base) setElementData(turret, "range_col", range_col) setElementData(turret, "weaponType", weaponType) setElementData(turret, "isProj", isProj) attachElements(turret, base, xOffset, yOffset, zOffset) end function setTurretTarget(id, md) if getElementData(source, "turret_part") == true then local turret = getElementData(source, "parent") local isProj = getElementData(turret, "isProj") if not isProj then setElementData(turret, "target", id) triggerClientEvent(getRootElement(), "onSetTarget", turret, turret, id) else setElementData(turret, "target", id) triggerClientEvent(getRootElement(), "onSetTarget", turret, turret, id, true) end end end function stopTurret(id, md) if getElementData(source, "turret_part") == true then local turret = getElementData(source, "parent") local turret_target = getElementData(turret, "target") local isProj = getElementData(turret, "isProj") if id == turret_target then if isProj then triggerClientEvent(getRootElement(), "onIdle", turret, turret) else -- TODO end end end end function TurretKill(total_ammo, killer, killerWep, bodyP) if getElementData(killer, "turret") == true then triggerClientEvent(getRootElement(), "onIdle", killer, killer) end end -- Events addEventHandler("onColShapeHit", getRootElement(), setTurretTarget) addEventHandler("onColShapeLeave", getRootElement(), stopTurret) addEventHandler("customizeNewTurret", getRootElement(), SetTurretData) addEventHandler("onPlayerWasted", getRootElement(), TurretKill) TurretClient.lua -- Client Functions addEvent("onTurretCreate", true) function createNewTurret(x, y, z, base, range_col, weaponType, isProj, range, xOffset, yOffset, zOffset) local turret = createWeapon(weaponType, x, y, z) setWeaponFlags(turret, "instant_reload", true) triggerServerEvent("customizeNewTurret", turret, turret, base, range_col, weaponType, isProj, range, xOffset, yOffset, zOffset) end addEvent("onSetTarget", true) function SetTurretTarget(turret, target, isProj) if not isProj then setWeaponTarget(turret, target) setTimer(setWeaponState, math.random(1500, 2500), 1, turret, "firing") else --createProjectile() end end addEvent("onIdle", true) function SetTurretIdle(turret, isProj) if not isProj then setWeaponState(turret, "ready") setTimer(setWeaponState, math.random(1000, 2000), 1, turret, "reloading") end end function checkTurret() if getElementData(source, "isProj") == true then cancelEvent() end end -- Events addEventHandler("onTurretCreate", getRootElement(), createNewTurret) addEventHandler("onSetTarget", getRootElement(), SetTurretTarget) addEventHandler("onIdle", getRootElement(), SetTurretIdle) addEventHandler("onClientWeaponFire", getRootElement(), checkTurret) Help please? Link to comment
Buffalo Posted October 7, 2015 Share Posted October 7, 2015 createWeapon is client side, you must create turret for every client by syncing position using serverside triggering. Store positions in serverside array. Update newly joined clients where turrets are placed and their state. Sync turret state among all clients using triggering. You also can sync without triggering but detecting clientside colshape hits when other players enetrs them. Tho target changing might be less tricky doing serverside syncing what turret is targeting. You can not send client side elements to server! Instead send what is current turret target. Link to comment
louie999 Posted October 7, 2015 Author Share Posted October 7, 2015 Ok, I think I kinda get how it works now. Tho, how would I update the turrets to newly joined players? should I just get all turret positions and create a turret for specifically the new player? Link to comment
Moderators IIYAMA Posted October 7, 2015 Moderators Share Posted October 7, 2015 To solve this problem you have to give the ped ammo, after it streams-in for a certain player. https://wiki.multitheftauto.com/wiki/On ... ntStreamIn Link to comment
louie999 Posted October 7, 2015 Author Share Posted October 7, 2015 Hmm, I've created a turret(using custom weapons) tho, it's a bit buggy atm ._. Even if I go out of the turret's colSphere it still attacks me D: also, how can I easily make the turret miss sometimes? A minigun turret with 100% accuracy is a bit over powered... I've tried making a table that will contain all elements that enter the turret's colSphere. Code: TurretClient.lua -- Client Functions function tableContains(tab, val) for _, v in pairs(tab) do if v == val then return true end end return false end addEvent("onTurretCreate", true) function createNewTurret(range_col, base, owner, team, x, y, z, weaponType, isProj, range, xOffset, yOffset, zOffset) local turret = createWeapon(weaponType, x, y, z) setWeaponFlags(turret, "instant_reload", true) setElementData(range_col, "parent_turret", turret) setElementData(turret, "owner", owner) setElementData(turret, "team", team) setElementData(turret, "turret", true) setElementData(turret, "base", base) setElementData(turret, "range_col", range_col) setElementData(turret, "weaponType", weaponType) setElementData(turret, "isProj", isProj) setElementData(turret, "targets", {}) attachElements(turret, base, xOffset, yOffset, zOffset) triggerServerEvent("UpdateTurretData", getRootElement(), owner, team, x, y, z, weaponType, isProj, range, xOffset, yOffset, zOffset) end function turretShoot(turret, target) local x, y, z = getElementPosition(turret) local tx, ty, tz = getElementPosition(target) local canAttackPts = 0 local owner = getElementData(turret, "owner") local team = getElementData(turret, "team") if owner ~= nil then if getPlayerName(target) ~= owner then canAttackPts = canAttackPts + 1 end else canAttackPts = canAttackPts + 1 end if team ~= nil then local pTeam = getPlayerTeam(target) local pTeam = getTeamName(pTeam) if pTeam ~= team then canAttackPts = canAttackPts + 1 end else canAttackPts = canAttackPts + 1 end if canAttackPts >= 2 then setWeaponTarget(turret, target) setTimer(setWeaponState, 1500, 1, turret, "firing") else turretLookForNewTarget(turret) end end function turretSetIdle(turret) setWeaponState(turret, "ready") setTimer(setWeaponState, 1000, 1, turret, "reloading") end function turretLookForNewTarget(turret) local targets = getElementData(turret, "targets") if targets[1] ~= nil then local target = math.random(targets[1], targets[#targets]) turretSetIdle(turret) setTimer(turretShoot, 2500, 1, turret, target) else turretSetIdle(turret) end end addEvent("onTurretFindTarget", true) function turretSetTarget(range_col, target) local turret = getElementData(range_col, "parent_turret") table.insert(getElementData(turret, "targets"), target) turretShoot(turret, target) end addEvent("onTurretTargetEscape", true) function turretTargetEscape(range_col) local turret = getElementData(range_col, "parent_turret") local targets = getElementData(turret, "targets") local target = getElementData(turret, "target") if tabContains(targets, target) then table.remove(targets, target) end turretLookForNewTarget(turret) end -- Events addEventHandler("onTurretCreate", getRootElement(), createNewTurret) addEventHandler("onTurretFindTarget", getRootElement(), turretSetTarget) addEventHandler("onTurretTargetEscape", getRootElement(), turretTargetEscape) 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