CobbTheWarriorsRPG Posted February 26, 2015 Share Posted February 26, 2015 Hello, i have a problem, i need help with this code, im trying to make a resource, wich when you do "stunt", it gives x money to you acording to the distance, here the code: local root = getRootElement() local resourceRoot = getResourceRootElement(getThisResource()) local textTable = {} addEvent("onPlayerStuntComplete", true) function onStuntStart_server(resource) local players = getElementsByType("player") for i,v in ipairs(players) do createPlayerDisplay(v) end end function createPlayerDisplay(player) textTable[player] = {} textTable[player].display = textCreateDisplay() textTable[player].textitem = textCreateTextItem ("", .5, .85, "low", 255, 255, 255, 255, 1.5, "center") textDisplayAddText(textTable[player].display, textTable[player].textitem) setElementData(player, "stuntTextTimer", false) end function onPlayerJoin_server() createPlayerDisplay(source) end function onPlayerQuit_server(reason) -- kill the timer of the current display if it exists local oldTimer = getElementData(source, "stuntTextTimer") if (oldTimer) then killTimer(oldTimer) end -- destroy text objects and clear the player's table textDestroyTextItem(textTable[source].textitem) textDestroyDisplay(textTable[source].display) textTable[source] = nil end function onStunt(type, vehicle, time, distance, height, rotation, greatLanding) assert(vehicle and isElement(vehicle), "omgz i cant see the vehicle") assert(source and isElement(source), "omgz i cant see the player") -- get rid of the current display if it exists local oldTimer = getElementData(source, "stuntTextTimer") if (oldTimer) then killTimer(oldTimer) hidePlayerDisplay(source) end -- create the new display local string if (type == "Jump") then string = "JUMP BONUS $50! Distance: " .. distance .. " Height: " .. height .. " Rotation: " .. rotation .. "º Time: " .. time .. "s" givePlayerMoney (source, 50) if (greatLanding) then string = string .. "\nLANDING BONUS 20$! What a great landing!" givePlayerMoney (source, 20) end else if (type == "Two-Wheeler") then type = "TWO-WHEELER" elseif (type == "Wheelie") then type = "WHEELIE" elseif (type == "Stoppie") then type = "STOPPIE" else error("unexpected stunt type recieved") end string = type .. " BONUS 70$! Distance: " .. distance .. " Time: " .. time if " .. distance .. " >= 100 then givePlayerMoney (source, 70) end end textItemSetText(textTable[source].textitem, string) textDisplayAddObserver(textTable[source].display, source) local timer = setTimer(hidePlayerDisplay, 5000, 1, source) setElementData(source, "stuntTextTimer", timer) end function hidePlayerDisplay(player) textDisplayRemoveObserver(textTable[player].display, player) setElementData(player, "stuntTextTimer", false) end addEventHandler("onResourceStart", resourceRoot, onStuntStart_server) addEventHandler("onPlayerJoin", root, onPlayerJoin_server) addEventHandler("onPlayerQuit", root, onPlayerQuit_server) addEventHandler("onPlayerStuntComplete", root, onStunt) Lot of thanks!. Link to comment
xXMADEXx Posted February 27, 2015 Share Posted February 27, 2015 You never addressed the problem? What is it? Link to comment
CobbTheWarriorsRPG Posted February 27, 2015 Author Share Posted February 27, 2015 You never addressed the problem? What is it? Ohh made, Sorry. Here is the problem Line 62 to 64, im tring to do this: if the distance is the same or more to 100 on a wheelie it gives to you 70 bucks! local root = getRootElement() local resourceRoot = getResourceRootElement(getThisResource()) local textTable = {} addEvent("onPlayerStuntComplete", true) function onStuntStart_server(resource) local players = getElementsByType("player") for i,v in ipairs(players) do createPlayerDisplay(v) end end function createPlayerDisplay(player) textTable[player] = {} textTable[player].display = textCreateDisplay() textTable[player].textitem = textCreateTextItem ("", .5, .85, "low", 255, 255, 255, 255, 1.5, "center") textDisplayAddText(textTable[player].display, textTable[player].textitem) setElementData(player, "stuntTextTimer", false) end function onPlayerJoin_server() createPlayerDisplay(source) end function onPlayerQuit_server(reason) -- kill the timer of the current display if it exists local oldTimer = getElementData(source, "stuntTextTimer") if (oldTimer) then killTimer(oldTimer) end -- destroy text objects and clear the player's table textDestroyTextItem(textTable[source].textitem) textDestroyDisplay(textTable[source].display) textTable[source] = nil end function onStunt(type, vehicle, time, distance, height, rotation, greatLanding) assert(vehicle and isElement(vehicle), "omgz i cant see the vehicle") assert(source and isElement(source), "omgz i cant see the player") -- get rid of the current display if it exists local oldTimer = getElementData(source, "stuntTextTimer") if (oldTimer) then killTimer(oldTimer) hidePlayerDisplay(source) end -- create the new display local string if (type == "Jump") then string = "JUMP BONUS $50! Distance: " .. distance .. " Height: " .. height .. " Rotation: " .. rotation .. "º Time: " .. time .. "s" givePlayerMoney (source, 50) if (greatLanding) then string = string .. "\nLANDING BONUS 20$! What a great landing!" givePlayerMoney (source, 20) end else if (type == "Two-Wheeler") then type = "TWO-WHEELER" elseif (type == "Wheelie") then type = "WHEELIE" elseif (type == "Stoppie") then type = "STOPPIE" else error("unexpected stunt type recieved") end string = type .. " BONUS 70$! Distance: " .. distance .. " Time: " .. time if " .. distance .. " >= 100 then givePlayerMoney (source, 70) end end textItemSetText(textTable[source].textitem, string) textDisplayAddObserver(textTable[source].display, source) local timer = setTimer(hidePlayerDisplay, 5000, 1, source) setElementData(source, "stuntTextTimer", timer) end function hidePlayerDisplay(player) textDisplayRemoveObserver(textTable[player].display, player) setElementData(player, "stuntTextTimer", false) end addEventHandler("onResourceStart", resourceRoot, onStuntStart_server) addEventHandler("onPlayerJoin", root, onPlayerJoin_server) addEventHandler("onPlayerQuit", root, onPlayerQuit_server) addEventHandler("onPlayerStuntComplete", root, onStunt) Link to comment
Addlibs Posted February 27, 2015 Share Posted February 27, 2015 --Remove lines 62 to 64 and replace with this. if distance >= 100 then string = type .. " BONUS 70$! Distance: " .. distance .. " Time: " .. time givePlayerMoney (source, 70) end And I would not suggest using variables such as 'type' or 'string' as they're pre-defined and you might lose access to functions such as string.format when 'string' is set to a string value instead of the original table. 'type' is used to check the type of a value (eg. nil, boolean, string, integer, userdata [usually elements]) and 'string' is a table containing some very useful string operation functions. Link to comment
CobbTheWarriorsRPG Posted February 27, 2015 Author Share Posted February 27, 2015 @MrTasty ERRO: stunt\main_server.lua atempt to compare a number whit string And thanks for the help bro! Link to comment
Addlibs Posted February 27, 2015 Share Posted February 27, 2015 try if tonumber(distance) >= 100 then Link to comment
CobbTheWarriorsRPG Posted February 27, 2015 Author Share Posted February 27, 2015 A lots of thanks @Mrtasty Its works! but... im trying to add more distances, but it doesnt work see men if tonumber(distance) >= 100 then string = type .. " BONUS 70$! Distance: " .. distance .. " Time: " .. time givePlayerMoney (source, 70) elseif tonumber(distance) >= 150 then string = type .. " BONUS 1000$! Distance: " .. distance .. " Time: " .. time givePlayerMoney (source, 1000) end Link to comment
Addlibs Posted February 28, 2015 Share Posted February 28, 2015 Start from the highest and go down to lowest. Lua executes the first matching if statement - if the >100 returns true, that code is executed and rest is ignored. Link to comment
CobbTheWarriorsRPG Posted March 6, 2015 Author Share Posted March 6, 2015 Start from the highest and go down to lowest. Lua executes the first matching if statement - if the >100 returns true, that code is executed and rest is ignored. A lot of thanks bro!, it works! And thanks for solve my question! 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