Program will only run once?

Hi everyone,

I’m creating a marble tag-style game. My current code to tag another user works once, but then it stops working with no errors. I have tested with a part that should work with the code AND another client. Does not work either way.

code:

“ballgamescript”:

print("ballgamescript loaded")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		_G.isTag = true
		local HRP = char:WaitForChild("HumanoidRootPart")
		local marble = Instance.new("Part")
		marble.Name = "marblehit"
		marble.Size = Vector3.new(10,10,10)
		marble.Transparency = .5
		marble.Shape = Enum.PartType.Ball
		marble.Parent = char
		marble.Material = Enum.Material.SmoothPlastic
		local Velocity = Instance.new("BodyAngularVelocity")
		Velocity.Parent = marble
		local Hum = char:WaitForChild("Humanoid")
		local Weld = Instance.new("Weld")
		Weld.Parent = marble
		Weld.Part0 = HRP
		Weld.Part1 = marble
		Hum.PlatformStand = true
		
		while true do
			wait()
			marble.BodyAngularVelocity.AngularVelocity = Vector3.new(char.Humanoid.MoveDirection.z * 32,0,char.Humanoid.MoveDirection.x * -32)
			marble.BodyAngularVelocity.MaxTorque = Vector3.new(10000,10000,10000)
			if char.Humanoid.MoveDirection == Vector3.new(0,0,0) then
				marble.BodyAngularVelocity.MaxTorque = Vector3.new(0,0,0)
			end
		end
		
		if _G.isTag == true then
			marble.BrickColor = BrickColor.new("Black")
			
		else
			marble.BrickColor = BrickColor.Random()
		end
	end)
end)

“tagscript”

print("tag loaded")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("marblehit").Touched:Connect(function(hit)
			if hit:IsA("Part") and hit.Name == "marblehit" then
				if _G.isTag == true then
					hit.BrickColor = BrickColor.new("Black")
					_G.isTag = false
					character:WaitForChild("marblehit").BrickColor = BrickColor.Random()
				end
			end
		end)
	end)
end)

output:

  16:22:51.349  Bubble Bombs tech demo @ 17 Oct 2022 16:22 auto-recovery file was created  -  Studio
  16:22:53.076  ballgamescript loaded  -  Server - bgsdemo:1
  16:22:53.076  tag loaded  -  Server - tagdemo:1
  16:22:55.121  Requiring asset 512742721.
Callstack:
cloud_8428896693.archimedesTwo.initiatePlugin.MainModule, line 29
cloud_8428896693.archimedesTwo.initiatePlugin.MainModule, line 28
  -  Server
  16:22:55.164  initialized  -  Server
  16:22:55.338  Requiring asset 518094091.
Callstack:
4 Likes

Looking at the output, it says Requiring asset, are you using a ModuleScript? those usually only run once.

1 Like

No, but a plugin I use might. All of my code in in normal server scripts

try adding threading inside the PlayerAdded events

local Players = game:GetService("Players")


local function OnPlayerAdded(Player)
      task.spawn(function()
             -- code in here
      end)
end


Players.PlayerAdded:Connect(OnPlayerAdded)

Still does not work- I spawn in 2 clients and tag the other client. It changes the other client’s colour to black, but when the now tagged client touches the original client it does not work.

Just a heads up:

if _G.isTag == true then
			marble.BrickColor = BrickColor.new("Black")
			
		else
			marble.BrickColor = BrickColor.Random()
		end

will never fire, because an unbreaking While loop is before it

Maybe something like this?

while true do
			wait()
			marble.BodyAngularVelocity.AngularVelocity = Vector3.new(char.Humanoid.MoveDirection.z * 32,0,char.Humanoid.MoveDirection.x * -32)
			marble.BodyAngularVelocity.MaxTorque = Vector3.new(10000,10000,10000)
			if char.Humanoid.MoveDirection == Vector3.new(0,0,0) then
				marble.BodyAngularVelocity.MaxTorque = Vector3.new(0,0,0)
			end
if _G.isTag == true then
			marble.BrickColor = BrickColor.new("Black")
			
		else
			marble.BrickColor = BrickColor.Random()
		end

		end
		
		

1 Like

Fixed it to run this way, but it still does not tag the other player.

do you any errors/warnings in your dev console while running the server with 2 or more players, also print is a useful debug tool, try adding prints to multiple parts in your code and see which prints are not getting called