. . . Posted October 30, 2016 Share Posted October 30, 2016 (edited) Hello, I need help . So, i'm trying to give the 1st two math randoms a 25% chance of happening & the rest of the math randoms a 12.5% chance of happening but sometimes when I activate this I don't get anything ? I'm sure I did my math right... Why is this happening? How can I make it that one of these 100% will activate? if math.random() < 0.25 then inst.components.lootdropper:SpawnLootPrefab("humanmeat") inst.components.lootdropper:SpawnLootPrefab("monstermeat") elseif math.random() < 0.25 then inst.components.lootdropper:SpawnLootPrefab("boneshard") inst.components.lootdropper:SpawnLootPrefab("humanmeat") inst.components.lootdropper:SpawnLootPrefab("monstermeat") elseif math.random() < 0.125 then inst.components.lootdropper:SpawnLootPrefab("humanmeat") inst.components.lootdropper:SpawnLootPrefab("humanmeat") elseif math.random() < 0.125 then inst.components.lootdropper:SpawnLootPrefab("monstermeat") inst.components.lootdropper:SpawnLootPrefab("monstermeat") elseif math.random() < 0.125 then inst.components.lootdropper:SpawnLootPrefab("boneshard") inst.components.lootdropper:SpawnLootPrefab("humanmeat") inst.components.lootdropper:SpawnLootPrefab("humanmeat") elseif math.random() < 0.125 then inst.components.lootdropper:SpawnLootPrefab("boneshard") inst.components.lootdropper:SpawnLootPrefab("monstermeat") inst.components.lootdropper:SpawnLootPrefab("monstermeat") end Thanks for reading my problem ! EDIT: Other problem is below! Edited October 30, 2016 by SuperDavid Link to comment https://forums.kleientertainment.com/forums/topic/71262-solved-need-help-with-mathrandom/ Share on other sites More sharing options...
chromiumboy Posted October 30, 2016 Share Posted October 30, 2016 Each time it sees a math.random() it generates a new number. You have no else statement, so if all the rolls fail, nothing will happen (roughly a 1 in 3 chance) Link to comment https://forums.kleientertainment.com/forums/topic/71262-solved-need-help-with-mathrandom/#findComment-831027 Share on other sites More sharing options...
. . . Posted October 30, 2016 Author Share Posted October 30, 2016 Oh! So, if I just remove one of the elseif & make it into a else everything will be fixed? Thanks so much! I didn't even remember I could use "else" ... Link to comment https://forums.kleientertainment.com/forums/topic/71262-solved-need-help-with-mathrandom/#findComment-831028 Share on other sites More sharing options...
Leonardo Cox Posted October 30, 2016 Share Posted October 30, 2016 have something = math.random() local dropchance = math.random() Also you're probably getting nothing because you have nothing set for when math.random goes over these numbers. Are you trying to make something drop random loot? Link to comment https://forums.kleientertainment.com/forums/topic/71262-solved-need-help-with-mathrandom/#findComment-831029 Share on other sites More sharing options...
. . . Posted October 30, 2016 Author Share Posted October 30, 2016 Just now, DarkKingBoo said: Are you trying to make something drop random loot? Yes, I have some state which I want it to drop these combinations of items as random loot, but everything should be good now ! Link to comment https://forums.kleientertainment.com/forums/topic/71262-solved-need-help-with-mathrandom/#findComment-831030 Share on other sites More sharing options...
Leonardo Cox Posted October 30, 2016 Share Posted October 30, 2016 This probably isn't exactly what you want but here is how spiders drop their random loot: inst.components.lootdropper:AddRandomLoot("monstermeat", 1) inst.components.lootdropper:AddRandomLoot("silk", .5) inst.components.lootdropper:AddRandomLoot("spidergland", .5) inst.components.lootdropper.numrandomloot = 1 the 2nd value being chance of the item dropping. Link to comment https://forums.kleientertainment.com/forums/topic/71262-solved-need-help-with-mathrandom/#findComment-831031 Share on other sites More sharing options...
. . . Posted October 30, 2016 Author Share Posted October 30, 2016 (edited) I have another problem if somebody could help me out that would be great ! So, here's the code. The problem is there's a chance of nothing happening when my char triggers this code while sane... if act.target.components.sanity:IsSane() and math.random() < 0.25 then act.target.components.talker:Say("Uh–uh!") elseif act.target.components.sanity:IsSane() and math.random() < 0.25 then act.target.components.talker:Say("I can't believe you!") elseif act.target.components.sanity:IsSane() and math.random() < 0.25 then act.target.components.talker:Say("You're joking, right?!") act.target:PushEvent("emote", { anim = "emoteXL_facepalm", mounted = true }) elseif act.target.components.sanity:IsSane() and math.random() < 0.25 then act.target.components.talker:Say("How dare you!") elseif not act.target.components.sanity:IsSane() then act.target.components.talker:Say("RaRArGH, NOoOooO!!!") end And if I do something like... Spoiler if act.target.components.sanity:IsSane() and math.random() < 0.25 then act.target.components.talker:Say("Uh–uh!") elseif act.target.components.sanity:IsSane() and math.random() < 0.25 then act.target.components.talker:Say("I can't believe you!") elseif act.target.components.sanity:IsSane() and math.random() < 0.25 then act.target.components.talker:Say("You're joking, right?!") act.target:PushEvent("emote", { anim = "emoteXL_facepalm", mounted = true }) elseif act.target.components.sanity:IsSane() and math.random() < 0.25 then act.target.components.talker:Say("How dare you!") else act.target.components.talker:Say("No!") elseif not act.target.components.sanity:IsSane() then act.target.components.talker:Say("RaRArGH, NOoOooO!!!") end It overrides this code, which's not good ... elseif not act.target.components.sanity:IsSane() then act.target.components.talker:Say("RaRArGH, NOoOooO!!!") So, can someone tell how I can prevent this ? Also, thanks so much for reading my problem, have a wonderful day/night ! Edited October 30, 2016 by SuperDavid Link to comment https://forums.kleientertainment.com/forums/topic/71262-solved-need-help-with-mathrandom/#findComment-831052 Share on other sites More sharing options...
Leonardo Cox Posted October 30, 2016 Share Posted October 30, 2016 (edited) Try this. local saychance = math.random(4) if act.target.components.sanity:IsSane() and saychance == 1 then act.target.components.talker:Say("Uh–uh!") elseif act.target.components.sanity:IsSane() and saychance == 2 then act.target.components.talker:Say("I can't believe you!") elseif act.target.components.sanity:IsSane() and saychance == 3 then act.target.components.talker:Say("You're joking, right?!") act.target:PushEvent("emote", { anim = "emoteXL_facepalm", mounted = true }) elseif act.target.components.sanity:IsSane() and saychance == 4 then act.target.components.talker:Say("How dare you!") elseif not act.target.components.sanity:IsSane() then act.target.components.talker:Say("RaRArGH, NOoOooO!!!") end Edited October 30, 2016 by DarkKingBoo Fixed Link to comment https://forums.kleientertainment.com/forums/topic/71262-solved-need-help-with-mathrandom/#findComment-831053 Share on other sites More sharing options...
. . . Posted October 30, 2016 Author Share Posted October 30, 2016 5 minutes ago, DarkKingBoo said: Try this. I tried it & there was still a chance that he didn't say anything while sane. And when he was insane he rarely would say anything. I appreciate your help though ! Link to comment https://forums.kleientertainment.com/forums/topic/71262-solved-need-help-with-mathrandom/#findComment-831055 Share on other sites More sharing options...
Leonardo Cox Posted October 30, 2016 Share Posted October 30, 2016 When does this code get triggered? Link to comment https://forums.kleientertainment.com/forums/topic/71262-solved-need-help-with-mathrandom/#findComment-831062 Share on other sites More sharing options...
. . . Posted October 30, 2016 Author Share Posted October 30, 2016 local SACRIFICE = GLOBAL.Action() SACRIFICE.str = "Sacrifice" SACRIFICE.id = "SACRIFICE" SACRIFICE.fn = function(act) if act.target.components.hunger.current <= 40 or act.target.components.health.currenthealth <= 100 then if act.target.components.sanity:IsSane() and math.random() < 0.25 then act.target.components.talker:Say("Uh–uh!") elseif act.target.components.sanity:IsSane() and math.random() < 0.25 then act.target.components.talker:Say("I can't believe you!") elseif act.target.components.sanity:IsSane() and math.random() < 0.25 then act.target.components.talker:Say("You're joking, right?!") act.target:PushEvent("emote", { anim = "emoteXL_facepalm", mounted = true }) elseif act.target.components.sanity:IsSane() and math.random() < 0.25 then act.target.components.talker:Say("What are you, an sadist?") elseif not act.target.components.sanity:IsSane() then act.target.components.talker:Say("We pReSeRvE oUr LiFe, NoT eNd It, fOoL!!!") end elseif act.target.components.hunger.current >= 40 and act.target.components.health.currenthealth >= 100 then if act.target:HasTag("playerghost") or act.target.components.health:IsDead() or act.target.sg:HasStateTag("giving") or act.target.sg:HasStateTag("busy") or act.target.sg:HasStateTag("working") or act.target.sg:HasStateTag("fishing") or act.target.sg:HasStateTag("doing") or act.target.sg:HasStateTag("playing") or act.target.sg:HasStateTag("running") or act.target.components.inventory:IsHeavyLifting() then return end act.target.sg:GoToState("sacrifice") end end AddAction(SACRIFICE) Link to comment https://forums.kleientertainment.com/forums/topic/71262-solved-need-help-with-mathrandom/#findComment-831066 Share on other sites More sharing options...
Leonardo Cox Posted October 30, 2016 Share Posted October 30, 2016 (edited) --Noticed that elseif check for the hunger was >= 40 and >= 100. You should probably just do > instead. It could cause a chance of nothing happening. --For the meantime I just made it else. --Sorry if this doesn't fix it, but I added some prints to let you know if the function is running properly when you don't say anything. local SACRIFICE = GLOBAL.Action() SACRIFICE.str = "Sacrifice" SACRIFICE.id = "SACRIFICE" SACRIFICE.fn = function(act) local saychance = math.random(1,4) if act.target.components.hunger.current <= 40 or act.target.components.health.currenthealth <= 100 then print("hunger values and health values met") if act.target.components.sanity:IsSane() and saychance == 1 then act.target.components.talker:Say("Uh–uh!") elseif act.target.components.sanity:IsSane() and saychance == 2 then act.target.components.talker:Say("I can't believe you!") elseif act.target.components.sanity:IsSane() and saychance == 3 then act.target.components.talker:Say("You're joking, right?!") act.target:PushEvent("emote", { anim = "emoteXL_facepalm", mounted = true }) elseif act.target.components.sanity:IsSane() and saychance == 4 then act.target.components.talker:Say("What are you, an sadist?") elseif not act.target.components.sanity:IsSane() then act.target.components.talker:Say("We pReSeRvE oUr LiFe, NoT eNd It, fOoL!!!") end else print("hunger values and health values NOT met") if act.target:HasTag("playerghost") or act.target.components.health:IsDead() or act.target.sg:HasStateTag("giving") or act.target.sg:HasStateTag("busy") or act.target.sg:HasStateTag("working") or act.target.sg:HasStateTag("fishing") or act.target.sg:HasStateTag("doing") or act.target.sg:HasStateTag("playing") or act.target.sg:HasStateTag("running") or act.target.components.inventory:IsHeavyLifting() then return end act.target.sg:GoToState("sacrifice") end end AddAction(SACRIFICE) Edited October 30, 2016 by DarkKingBoo Link to comment https://forums.kleientertainment.com/forums/topic/71262-solved-need-help-with-mathrandom/#findComment-831071 Share on other sites More sharing options...
. . . Posted October 30, 2016 Author Share Posted October 30, 2016 Thanks DarkKingBoo that seems to have done the trick !! Thanks so much !! Link to comment https://forums.kleientertainment.com/forums/topic/71262-solved-need-help-with-mathrandom/#findComment-831072 Share on other sites More sharing options...
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