Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there a way to use a preset value for finding an item in the workspace or in a diffrent location?

Is there a way to use a pre set varible / value for finding an item in replicated storage? This is what I have its not working.

local RepStore = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local EventsStor = RepStore:WaitForChild("Events")
local WeapChoose = EventsStor.WeaponChoose

local WeaponStor = RepStore.Weapons

WeapChoose.OnServerEvent:Connect(function(Plr,WeaponVal)
	print(WeaponVal)
	local ActiveWeapon = WeaponStor.WeaponVal:Clone() -- (This is where im questioning)
	--ActiveWeapon.Parent = game.StarterPack
	
	for _,v in pairs(Players:GetPlayers()) do
		ActiveWeapon.Parent = v.Backpack
		v:LoadCharacter()
	end
end)

1 answer

Yes. In general you can say things like

local weaponName = "WeaponNameHere"
local weapon = WeaponStor[weaponName]:Clone()

In Lua, the syntax EventsStor.WeaponChoose means the same thing as EventsStor["WeaponChoose"].

Note that you should never trust arguments sent in by the player (other than which player sent it in), such as WeaponVal - an exploiter could send arbitrary values and break your code or trick it into doing something disastrous.

In your case, you might benefit from letting the client scripts send in a reference to the weapon and then simply verifying that the value is an instance and its parent is WeaponStor:

WeapChoose.OnServerEvent:Connect(function(Plr,WeaponVal)
	if typeof(WeaponVal) ~= "Instance" or WeaponVal.Parent ~= WeaponStor then error("WeaponVal: " .. tostring(WeaponVal)) end
	local ActiveWeapon = WeaponVal:Clone()
	-- rest of your code

This discussion is preserved as it was. New answers, comments, and votes are closed.