Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[Script] Summoning Script - "controlledby" variable not working

I'm struggling to make a variable which will assign the correct user as the controller of a token. I can hard-assign players just fine, but for some reason it keeps assigning me as the controller, even when slice[2] should be someone else. Here's some snippets of the code: (playerids and character names removed for privacy/security reasons) var controller = "nobody"; if (slice[2] != undefined) { if (slice[2].toLowerCase() == "player1") { controller = [playerid]; } if (slice[2].toLowerCase() == "player2") { controller = [playerid]; } if (slice[2].toLowerCase() == "player3" || "player4") { controller = [playerid]; //two players are sharing a machine/account } else { controller = [playerid]"; //Dungeon Master } } if (slice[1].toLowerCase() == 'placeholder') { createObj("graphic", { name: "Placeholder", controlledby: controller, left: tok.get("left")+70, top: tok.get("top"), width: 70, height: 70, bar1_value: 1, bar1_max: 1, bar2_value: 1, bar2_max: 1, showname: true, showplayers_name: true, showplayers_bar1: true, showplayers_bar2: true, showplayers_aura1: true, showplayers_aura2: true, aura2_radius: 0, aura2_square: true, imgsrc: "<a href="https://s3.amazonaws.com/files.d20.io/images/47531147/iaQFtK_rVmbCxtdnVH86fg/thumb.png?1518255628" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/47531147/iaQFtK_rVmbCxtdnVH86fg/thumb.png?1518255628</a>", //Player Token image pageid: tok.get("pageid"), layer: "objects" }); sendChat("Summon Script", "A Placeholder was summoned. Please fill in the correct information."); } The Macro Button is currently written as: !summon ?{Summon What?|Placeholder|AirElemental|EarthElemental|FireElemental|WaterElemental|GiantElk} ?{Who are you?|Player1|Player2|Player3|Player4} As always, many thanks in advance!
1578759175

Edited 1578759354
GiGs
Pro
Sheet Author
API Scripter
If it keeps assigning your id, that suggests there's something wrong with this if statement: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (slice[2].toLowerCase() == "player1") { controller = [playerid]; } if (slice[2].toLowerCase() == "player2") { controller = [playerid]; } if (slice[2].toLowerCase() == "player3" || "player4") { controller = [playerid]; //two players are sharing a machine/account } else { controller = [playerid]"; //Dungeon Master } I see one issue with it immediately. Imagine the activating player is player1, and walk through the code. At the first if statement, the test is true, so controller is set to player1's playerid. But the code doesnt stop there, it keeps going. at the next if statement, its false, so the continues. Then at the third if statement, it checks against player3 or player4, and again it's false - and because its false, it goes to the else statement, and sets controller to the GM, overwriting the previous assignment. It looks to me like this code will set the controller correctly for player3 and 4, but for everyone else will set it to the GM. The best way to fix this is to use else if &nbsp;(it's situations like this that if else exists for): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (slice[2].toLowerCase() == "player1") { controller = [playerid]; } else if (slice[2].toLowerCase() == "player2") { controller = [playerid]; } else if (slice[2].toLowerCase() == "player3" || "player4") { controller = [playerid]; //two players are sharing a machine/account } else { controller = [playerid]"; //Dungeon Master } This ensures that only one option will be picked. You could also use switch , but you have the code already written for an if else chain, so might as well use it. If after this fix, controller is still being set to GM, then you need to look at exactly what is in slice[2], it's likely there is something not matching there.
Wow, I didn't add the space after "else" in my original "else if" attempt. (To be fair I was a bit tired) Thank you! I'll try this out real quick to make sure it works. If it doesn't I'll let you know and we can proceed further from there. Works as intended
1578808030
GiGs
Pro
Sheet Author
API Scripter
great :)