So, what’s happening is that when I press my left mouse button down on Wizard, release it (the button goes up) on Healer, then click Wizard, sometimes it registers that I clicked Healer, and sometimes it registers that I clicked Knight. From what I understand, the reason it sometimes does Knight is because the Knight button is the first descendant of my GUI’s frame, so the game gets confused and just selects that one because it’s the first one indexed or something like that. I have no clue how to solve this. I just want to make it so that if I move my mouse around while clicking, it will still select the option I’m clearly clicking.
There is also a similar issue where I press down, drag my mouse on the button, then release the click on the same button, and it still selects a different option than what I want, but I’m assuming these issues probably have the same solution.
Here is my code (It is a local script located inside StarterPlayerScripts. I am positive the issue is with this script & the buttons, not any other script.):
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local localPlayer = Players.LocalPlayer
local playerGui = localPlayer:WaitForChild("PlayerGui")
local screenGui = playerGui:WaitForChild("StartScreen")
local menuFrame = screenGui:WaitForChild("Background")
local changeClassEvent = ReplicatedStorage:WaitForChild("ChangeClassEvent")
for _, child in ipairs(menuFrame:GetChildren()) do
if child:IsA("TextButton") or child:IsA("ImageButton") then
child.MouseButton1Click:Connect(function()
local chosenClass = string.gsub(child.Name, "Button", "")
changeClassEvent:FireServer(chosenClass)
end)
end
end
Here is a video of what I’m talking about (I pressed my mouse button down on the Wizard button, let it up on the Healer button, then clicked the Wizard button, and it selected Knight):
Here is another video, which shows me dragging my mouse while clicking and it thinking I pressed a different button:
Your provivded code isn’t the problem because there is no reason why that code wouldn’t work correctly. It must be your sever code. Could you please send the part that handles the team assigner function? Because we might be able to find the issue, but there is always a possibility you left a hard coded variable that make the team knight no matter what. Thank you.
Are you sure that your “KnightButton” actually says “knight”?
+, using GSUB to get the classname is quite the hardcoded technique. I would recommend simply writing a reference table between the buttons and their classes.
The issue could be the way Roblox resizes buttons, making the buttons overlap other buttons. Another issue could also just be in the script itself. I put something together real quick, you can see if it works. This won’t fix the drag issue as I am unsure what’s causing that and may need more time to look into it.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local startScreen = playerGui:WaitForChild("StartScreen")
local background = startScreen:WaitForChild("Background")
local changeClassEvent = ReplicatedStorage:WaitForChild("ChangeClassEvent")
for _, object in ipairs(background:GetChildren()) do
if object:IsA("GuiButton") then
local button = object
button.MouseButton1Click:Connect(function()
local className = button.Name:gsub("Button$", "")
changeClassEvent:FireServer(className)
end)
end
end
I actually did some tests. It turns out that the issue is with the teams. Turns out both my team-changing script and the script I’ve shown understand which button you’re clicking. I’m still not sure what the issue is, but as far as I can tell something to do with the teams not being changed to the one they’re being told to change to.
Here is the script I’ve been using for the teams:
local classChangeEvent = game.ReplicatedStorage:WaitForChild("ChangeClassEvent")
local CH = require(game.ServerScriptService:WaitForChild("ClassHandler"))
local Teams = game:GetService("Teams")
classChangeEvent.OnServerEvent:Connect(function(plr, chosenClass)
print(chosenClass)
local plrInfo = CH.new(plr, chosenClass)
if not plrInfo then
warn("Couldn't create "..plr.Name.."'s Class Information.")
return
end
print("Created "..plr.Name.."'s Class Information.")
plr.Team = Teams:WaitForChild(chosenClass)
end)
btw: No, it is not that I’ve somehow hardcoded it to always pick Knight. I’ve gotten other outcomes as well, but I just didn’t think it was necessary to show those, as it seeming to pick Knight when I click on the buttons weird was the main issue (and still is). As you’ll see in the video below this text, my team changer script knows which button I’m clicking, but still gives the wrong team.
btw btw: ClassHandler literally does nothing, it’s just a module I wrote that stores the player name and their class. I was going to do more than it, but then I started seeing this issue and had to halt any progress with the module for the time being.
Video of it giving me the wrong team even though the script understands which button I’m pressing: