The goal of this piece of code is to achieve an idea where players can go on top of a button and detect if the player’s character is on it or not. If the character is on the button, another button appears where other players can join in; similar to what a queue system is. To do this, a heartbeat event is used to check every frame to detect if a player is within a “zone”, that is the button. However, only 1 player, which is the last player to join, is able to do any changes. Print statements were implemented to see if the code was running okay and they all worked as expected. The next button however does not lit up and does not do any changes to it’s string values. When testing with 2 players the last player to join was the only one to do any changes, but the first could not. Once, the last player has left, the first player could do changes. I’m not sure if it’s my coding logic or something else. Many thanks for actually reading this!
Player1 (first player to join):
Player2 (last player to join):
Player1 (with Player2 no longer in the server):
Script:
local q1 = script.Parent.q1
local q2 = script.Parent.q2
local function isPointInZone(point, zonePart)
local relPoint = zonePart.CFrame:PointToObjectSpace(point)
return math.abs(relPoint.X) < zonePart.Size.X * 0.5 and math.abs(relPoint.Z) < zonePart.Size.Z * 0.5
end
function findnextbutton(currentPos)
local nextbuttonpos = currentPos + 1
for i,v in ipairs(script.Parent.q1:GetChildren())do
if v.pos.Value == nextbuttonpos then
return v
end
end
for i,v in ipairs(script.Parent.q2:GetChildren())do
if v.pos.Value == nextbuttonpos then
return v
end
end
end
function findprevbutton(currentPos)
local prevbuttonpos = currentPos - 1
for i,v in ipairs(script.Parent.q1:GetChildren())do
if v.pos.Value == prevbuttonpos then
return v
end
end
for i,v in ipairs(script.Parent.q2:GetChildren())do
if v.pos.Value == prevbuttonpos then
return v
end
end
end
function buttonTouched(part)
game:GetService("RunService").Heartbeat:Connect(function()
for _,plr in pairs(game.Players:GetChildren())do
local atzone = isPointInZone(plr.Character.PrimaryPart.Position, part)
if atzone == true and part.enabled.Value == true then
withinzone(part,plr)
else
notwithinzone(part,plr)
end
end
end)
end
function withinzone(part, plr)
part.plr.Value = plr.Name
buttonEffect(part)
print(plr.Name)
if part.pos.Value+1 <= 10 then
print("this is working")
local nextbutton = findnextbutton(part.pos.Value)
nextbutton.Transparency = 0
nextbutton.enabled.Value = true
end
end
function notwithinzone(part, plr)
if part.name == "1" and plr.Name == "Player1" then
print("1 is off by "..plr.Name)
end
part.plr.Value = "n/a"
nobuttonEffect(part)
if part.pos.Value+1 <= 10 then
local nextbutton = findnextbutton(part.pos.Value)
nextbutton.Transparency = 1
nextbutton.enabled.Value = false
end
end
function buttonEffect(part)
print("changing colour effect")
part.BrickColor = BrickColor.new("Medium stone grey")
part.Material = Enum.Material.Neon
end
function nobuttonEffect(part)
part.BrickColor = BrickColor.new("Dark stone grey")
part.Material = Enum.Material.Plastic
end
function settingup()
wait(3)
local q1parts = q1:GetChildren()
local q2parts = q2:GetChildren()
for i,v in ipairs(q1parts)do
local val = Instance.new("StringValue")
val.Parent = v
val.Name = "plr"
val.Value = "n/a"
local bool = Instance.new("BoolValue")
bool.Parent = v
bool.Name = "enabled"
bool.Value = false
if v.Name == "1" then
v.Transparency = 0
bool.Value = true
end
buttonTouched(v)
end
for i,v in ipairs(q2parts)do
v.Transparency = 1
local val = Instance.new("StringValue")
val.Parent = v
val.Name = "plr"
val.Value = "n/a"
local bool = Instance.new("BoolValue")
bool.Parent = v
bool.Name = "enabled"
bool.Value = false
buttonTouched(v)
end
end
settingup()
I’ve been stuck for 3 days trying to figure this out.