|
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 19 Jan 2016 11:19 PM |
| Google can be so magical at times... |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|