I have a working marble system, and am trying to make a hot-potato game in which you collide with each other to pass the ‘hot marble’ (turn BrickColor("Red")
, marble:SetAttribute("Hot", true)
).
Problem is, every variation of this system I’ve tried has not replicated correctly, only worked once, or been bogged down in many lines of useless code.
To visualize: A red marble with a Hot
attribute set to true
collides with another marble. That marble is now red and Hot
, while the originally Hot
marble is no longer red or Hot
.
Any help is appreciated. I’ve run into problem after problem.
Check on the server:
local function main(other_part)
other_part:SetAttribute("Hot", not other_part:GetAttribute("Hot"))
marble:SetAttribute("Hot", not marble:GetAttribute("Hot"))
-- Code to change appearance
end
marble.Touched:Connect(main)
The problem I find with that is, each marble is parented to the player’s character model, and so defining other_part
and marble
is tricky.
I’ve tried using local scripts to solve this but the RemoteEvents get tricky and out of sync with the client.
other_part
is a parameter automatically passed whenever a part’s Touched
event fires.
Therefore, you only need to define marble
:
local PLYRS = game:GetService("Players")
for k, v in pairs(PLYRS) do
local character = v.Character or v.CharacterAdded:Wait()
local marble = character:FindFirstChild("marble")
while not marble do
marble = character:FindFirstChild("marble")
task.wait()
end
marble.Touched:Connect(function(other_part)
-- my code here
end)
This works on the assumption that you create the marble on PlayerAdded
.
The marble is created on .CharacterAdded
, but since you have v.CharacterAdded:Wait()
, I think it will still work, I’ll test it out later, thank you for the help
2 Likes
I’ve made a few edits to the script to try color and debounce, but otherwise it’s the same- not working. The print()
s do not print (even "marble touched"
), there are no errors, and nothing occurs- the red, Hot
marble stays red and Hot
, the white and not Hot
marble stays white and not Hot
.
local debounce = false
for i, v in pairs(game:GetService("Players"):GetPlayers()) do
local char = v.Character or v.CharacterAdded:Wait()
local marble = char:FindFirstChild("Marble")
while not marble do
marble = char:FindFirstChild("Marble")
task.wait()
end
marble.Touched:Connect(function(h)
print("marble touched")
if h.Name == "Marble" and debounce == false then
debounce = true
h:SetAttribute("Hot", not h:GetAttribute("Hot"))
marble:SetAttribute("Hot", not marble:GetAttribute("Hot"))
print(marble.Parent.Name.." is "..marble:GetAttribute("Hot"))
print(h.Parent.Name.." is "..h:GetAttribute("Hot"))
if h.Color == Color3.new(255,0,0) then
marble.Color = Color3.new(255,0,0)
h.Color = Color3.new(255,255,255)
elseif h.Color == Color3.new(255,255,255) and marble.Color == Color3.new(255,0,0) then
h.Color = Color3.new(255,0,0)
marble.Color = Color3.new(255,255,255)
end
task.wait(1)
debounce = false
end
end)
end
If your prints
aren’t returning output, then your equality check has failed.
Is h.Name
“Marble”; does debounce
change values anywhere else?
Even "marble touched"
doesn’t print though, meaning marble.Touched
never fires
In which case, marble
is nil, and the thread is caught at the while-true loop.
I’d recommend printing the values of v
, char
and marble
before entering a loop.