Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

I can only click on one of my gui classes?

Asked by 2 years ago
Edited 2 years ago

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
0
Is your script in StarterPlayerScripts? Xapelize 2658 — 2y

2 answers

Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

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.

Ad
Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
2 years ago

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
0
I replaced my script with your script and it made it work but when i died it didnt keep the class.. And i removed while true do but then all of the classes didnt work Omgcat1111 12 — 2y
0
Is your script in StarterPlayerScripts? Xapelize 2658 — 2y
0
No the tutorial didnt do that Omgcat1111 12 — 2y
0
https://www.youtube.com/watch?v=t64gExqgPbM the tutorial is this so please tell me whats wrong Omgcat1111 12 — 2y

Answer this question