I have checked it and it is normal exactly the same as the other gui one but it just wont work when i click it.. It was fine at first but then I changed it to keep the element forever but now i can only select one.. Heres the code (please help me)
local rp = game:GetService ("ReplicatedStorage") local Class = rp:WaitForChild ("Class") local players = game:GetService("Players") local SS = game:GetService ("ServerStorage") local Classes = SS:WaitForChild("Classes") Class.OnServerEvent:Connect(function(Player,className) local Backpack = Player.Backpack local selection = Classes:FindFirstChild(className) if selection then local moveset = Classes:FindFirstChild(className):Clone() moveset.Parent = Backpack end end)
And the second code is this: local Background = script.Parent
local rp = game:GetService ("ReplicatedStorage") local Class = rp:WaitForChild ("Class") for _, button in pairs(Background:GetChildren()) do if button:IsA("ImageButton") then button.MouseButton1Click:Connect(function() Background.Visible = false Class:FireServer(button.Name) end) local player = game.Players.LocalPlayer local hum = player.Character:WaitForChild("Humanoid") while true do if hum.Health == 0 then wait(5) Class:FireServer(button.Name) return end wait(0.1) end end end
Your script is stopping because while true do
will begin running when it loops through the first button so next loops aren't ever going to begin, your solution is not use a loop, instead keep a track of what class user is on the server and when he spawns, give him that class.
local rp = game:GetService ("ReplicatedStorage") local Class = rp:WaitForChild ("Class") local players = game:GetService("Players") local SS = game:GetService ("ServerStorage") local Classes = SS:WaitForChild("Classes") local playerClasses = {} local function setClass(Player, className) local selection = Classes:FindFirstChild(className) if selection then local moveset = selection:Clone() -- I believe it's better to use WaitForChild on backpack -- since it caused weird behavior when not using it for me -- but it's up to you moveset.Parent = Player:WaitForChild("Backpack") end end Class.OnServerEvent:Connect(function(Player,className) setClass(Player, className) -- save last used player class into a table playerClasses[Player] = className end) players.PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(character) -- get last used player class from the table local className = playerClasses[player] -- If player does not have any previous class then -- just return and don't continue the function if not className then return end setClass(player, className) end) end) -- When player leaves remove his last saved class since -- it won't be used anymore so there is no point in saving -- it, removing this will cause memory leaks on big servers players.PlayerRemoving:Connect(function(player) playerClasses[player] = nil end)
And for the LocalScript:
local Background = script.Parent local rp = game:GetService("ReplicatedStorage") local Class = rp:WaitForChild("Class") for _, button in ipairs(Background:GetChildren()) do if button:IsA("ImageButton") then button.MouseButton1Click:Connect(function() Background.Visible = false Class:FireServer(button.Name) end) end end
Use ipairs for arrays, it improves performance.
Delete the while true do line in LocalScript. It's unneeded.
local rp = game:GetService ("ReplicatedStorage") local Class = rp:WaitForChild ("Class") for _, button in pairs(Background:GetChildren()) do if button:IsA("ImageButton") then button.MouseButton1Click:Connect(function() Background.Visible = false Class:FireServer(button.Name) end) local player = game.Players.LocalPlayer local hum = player.Character:WaitForChild("Humanoid") end end