So basically, I have this selection menu to select a gun and attachments. When you select a gun it sets a value to a string value within the ScreenGUI. That part works, the part that does not is when I try to set the variable in the module script of the gun. It sets the variable, but does not put the attachment of the gun.
-
What do you want to achieve? A successful way to clone a gun from the weapon pool, change the module script variable, and make the attachment appear on the gun when you equip it.
-
What is the issue? Attachment does not appear on the gun.
GUI
note: code is not fully done/optimized
Server Script
local sevent = game:GetService("ReplicatedStorage"):WaitForChild("Custom").Remotes.SelectionGUN
local aevent = game:GetService("ReplicatedStorage"):WaitForChild("Custom").Remotes.SelectionATT
local playevent = game:GetService("ReplicatedStorage"):WaitForChild("Custom").Remotes.Play
local rs = game:GetService("ReplicatedStorage")
local weaponpool = rs:WaitForChild("WeaponPool")
sevent.OnServerEvent:Connect(function(player, types, gun, reset)
local gui = player.PlayerGui
local selection = gui.MainMenu.Main.Selection
local bool = selection.attachmentboolean
--is reset?
if reset == true then
warn("SORT RESET")
for i, v in ipairs(bool:GetChildren()) do
if v:IsA("BoolValue") then
v.Value = false
end
end
for i, v in ipairs(selection:GetChildren()) do
if v:IsA("StringValue") then
v.Value = ""
end
for i, x in ipairs(v:GetChildren()) do
if v:IsA("StringValue") then
v.Value = ""
end
end
end
return
end
-- checks for invalids
if gun == nil or types == nil then
print("NIL")
return
end
--sets attachment bool to true
if bool[types].Value == false then
bool[types].Value = true
end
--sets the selector
if selection:FindFirstChild(types) then
selection[types].Value = gun
print(types .. " set to " .. gun)
end
end)
aevent.OnServerEvent:Connect(function(player, types, attachment, guntype)
local gui = player.PlayerGui
local selection = gui.MainMenu.Main.Selection
local bool = selection.attachmentboolean
--checks for invalids
if types == nil or attachment == nil then
print("NIL")
return
end
--checks if bool is true
if bool[types].Value == false then
print("please select a gun")
return
end
if selection:FindFirstChild(types) then
if selection:FindFirstChild(types):FindFirstChild(guntype) then
selection[types][guntype].Value = attachment
print(types, guntype, attachment)
print(selection[types][guntype].Value ) -- check if it set
end
end
end)
local function loadprimary(player, gun)
local gui = player.PlayerGui
local selection = gui.MainMenu.Main.Selection
local primaryf = selection.Primary
local primary = primaryf.Value
local primary_optic = primaryf.Optic.Value
local primary_barrel = primaryf.Barrel.Value
local primary_underbarrel = primaryf.Underbarrel.Value
local module = require(gun.ACS_Settings)
if primary_optic ~= nil then
module.SightAtt = primary_optic
print(module.SightAtt)
end
if primary_barrel ~= nil then
module.BarrelAtt = primary_barrel
end
if primary_underbarrel ~= nil then
module.UnderBarrelAtt = primary_underbarrel
end
return module
end
playevent.OnServerEvent:Connect(function(player)
local gui = player.PlayerGui
local selection = gui.MainMenu.Main.Selection
local hasprimary
local hassecondary
if selection.Primary.Value == "" or selection.Primary.Value == nil then
hasprimary = false
else
hasprimary = true
end
if selection.Secondary.Value == "" or selection.Secondary.Value == nil then
hassecondary = false
else
hassecondary = true
end
if hasprimary == true then
local primaryf = selection.Primary
local primary = primaryf.Value
local getgun = weaponpool.Primary:FindFirstChild(primary):Clone()
loadprimary(player, getgun)
getgun.Parent = player.Backpack
end
end)
GunSelector (Client)
local sevent = game:GetService("ReplicatedStorage"):WaitForChild("Custom").Remotes.SelectionGUN
local player = game.Players.LocalPlayer
local selection = player.PlayerGui.MainMenu.Main.Selection
local types = script.Parent.Parent.Parent.Name
local bool = selection.attachmentboolean
script.Parent.MouseButton1Click:Connect(function()
print(types)
print(script.Parent.Name)
if types == "Grenade" then
print('feature not added yet')
else
if types == "Knife" then
print('feature not added yet')
else
--PRIMARY/SECONDARY
print("PRIMARY/SECONDARY")
if types == "Primary" or types == "Secondary" then
for i, v in ipairs(script.Parent.Parent:GetChildren()) do
if v:IsA("TextButton") then
v.BackgroundColor3 = Color3.fromRGB(140, 243, 147)
end
end
script.Parent.BackgroundColor3 = Color3.new(0.952941, 1, 0.643137)
sevent:FireServer(types, script.Parent.Name, false)
end
end
end
end)
Attachment Selector (Client)
local aevent = game:GetService("ReplicatedStorage"):WaitForChild("Custom").Remotes.SelectionATT
local player = game.Players.LocalPlayer
local selection = player.PlayerGui.MainMenu.Main.Selection
local types = script.Parent.Parent.Parent.Name
local guntype = script.Parent.Parent.Name
local bool = selection.attachmentboolean
script.Parent.MouseButton1Click:Connect(function()
print(types, script.Parent.Name)
if bool[types].Value == false then
print("please select a gun first")
return
end
for i, v in ipairs(script.Parent.Parent:GetChildren()) do
if v:IsA("TextButton") then
v.BackgroundColor3 = Color3.fromRGB(140, 243, 147)
end
end
script.Parent.BackgroundColor3 = Color3.new(0.952941, 1, 0.643137)
aevent:FireServer(types, script.Parent.Name, guntype)
end)