generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: If statements and script efficiency

Previous Thread :: Next Thread 
SalazarSliytheryn is not online. SalazarSliytheryn
Joined: 17 Dec 2010
Total Posts: 19
19 Jan 2016 10:44 PM
I am writing a script for my game that gives players an item based off an intValue in their backpack and it looks kinda like this:

If intValue == 1 then
give player item1
elseif intValue == 2 then
give player item2

ect.

I was wondering if there was a more efficient way for the script to look at the value and give the player the correct item so that I don't have to write a bunch of if statements over and over again.
Report Abuse
LegendaryAccount is not online. LegendaryAccount
Joined: 02 Jun 2010
Total Posts: 13193
19 Jan 2016 10:51 PM
If you make the int value a string value and store the name of the tool as the string value you can do this

function GiveItem(player,ItemName)
local b = player.Backpack
local t = game:GetService("ReplicatedStorage")
t[ItemName]:Clone().Parent = b
end

GiveItem(game.Players.LocalPlayer,StringValue.Value)


Im defining player as if it was in a local script but this may be a server script so you should defind the player accordingly
the tool is should be located inside ReplicatedStorage or change it to be where your tools are located


Report Abuse
LegendaryAccount is not online. LegendaryAccount
Joined: 02 Jun 2010
Total Posts: 13193
19 Jan 2016 10:53 PM
or you can keep it the int value the same and change the name of the tool to the int value you assign it to so the tool would be named "1","2","3"


Report Abuse
AdvancedObjects is not online. AdvancedObjects
Joined: 11 Dec 2012
Total Posts: 3354
19 Jan 2016 10:55 PM
I'd personally prefer to do it Server Sided instead of Client Sided.



#Code print("Song Link: http://www.roblox.com/Deorro-vs-Swedish-House-Mafia-Save-The-5-Hours-item?id=340827217")
Report Abuse
SalazarSliytheryn is not online. SalazarSliytheryn
Joined: 17 Dec 2010
Total Posts: 19
19 Jan 2016 11:15 PM
This is being done on the server but I need it to check the value that the player has and give the item accordingly this is my current script:

--This script is getting all the items and their values from a ModuleScript in the Workspace

local items = require(game.Workspace.GameItems)

function get(player)
player.CharacterAdded:connect(function()
print(items.item1[1])
local slots = player.PlayerGui.ScreenGui.CurrentInventory
if slots.Slot1.Value.Value == 1 then
items.item1[1]:Clone().Parent = player.Backpack
elseif slots.Slot1.Value.Value == 2 then
items.item1[2]:Clone().Parent = player.Backpack
end
end)
end

game.Players.PlayerAdded:connect(get)

For this 'inventory' system I need to it give players the items based on the value in each slot of the players 'inventory' and I have 4 slots and since I will most likely add alot of items into the game later on I was just wondering if there was a more efficient way of doing all the value checks rather than using a bunch of if statements.
Report Abuse
RaulChaser is not online. RaulChaser
Joined: 07 Dec 2014
Total Posts: 655
19 Jan 2016 11:19 PM
Google can be so magical at times...
Report Abuse
SalazarSliytheryn is not online. SalazarSliytheryn
Joined: 17 Dec 2010
Total Posts: 19
19 Jan 2016 11:35 PM
M8 if I found the solution to this on google I wouldn't have put this on the forums
Report Abuse
AdvancedObjects is not online. AdvancedObjects
Joined: 11 Dec 2012
Total Posts: 3354
19 Jan 2016 11:38 PM
My statement was aimed at Legendary as he was doing it via Client Side.



#Code print("Song Link: http://www.roblox.com/Deorro-vs-Swedish-House-Mafia-Save-The-5-Hours-item?id=340827217")
Report Abuse
SalazarSliytheryn is not online. SalazarSliytheryn
Joined: 17 Dec 2010
Total Posts: 19
19 Jan 2016 11:56 PM
Sorry, I assumed as much. I was just giving more information because Legendary seemed to misunderstand what I was asking.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image