Why are these scripts only working in ROBLOX Studio?

Hey!

I am trying to spawn parts around the map randomly and one of them will kick all players in the game if it gets touched.

But the issue is whenever I test this in ROBLOX Studio it works but when I go ahead and test it in testing mode which is the real game and for some reason it won’t work, this is really weird and I hope that someone can help me, it also doesn’t give me any errors in the Output and that is the main reason as to why I can’t identify the problem with these scripts.

I have tried my best to fix the scripts as much as I can but it still won’t work in the testing mode a.k.a the main game it will only work in ROBLOX Studio as i mentioned.

Sorry if it’s something obvious I am new to scripting and I am still learning how to do stuff I was practicing with this and I found out that it’s not working in the main game but it only works in ROBLOX Studio so I wanted to know why.

Spawning parts randomly around the map
local fakepartsMaxX = 240 -- to spawn them randomly around the map
local fakepartsMinX = -240
local fakepartsMaxZ = 240
local fakepartsMinZ = -240

local fakepartsy = 2

math.randomseed(tick())

wait(5)

while #game.Players:GetPlayers() >= 2 do -- checking if the player count is 2 then executing the code by repeatedly spawning parts
	wait(1)
	local fakepartsx = math.random(fakepartsMinX, fakepartsMaxX)
	local fakepartsz = math.random(fakepartsMinZ, fakepartsMaxZ)
	local fakeparts = Instance.new("Part")
	fakeparts.Name = "FakePart"
	fakeparts.Parent = game.Workspace.parts
	fakeparts.Anchored = false
	fakeparts.CFrame = CFrame.new(fakepartsx,fakepartsy,fakepartsz)
end
The part that kicks people when they touch it
local realpartsMaxX = 240 -- to spawn parts around the map randomly
local realpartsMinX = -240
local realpartsMaxZ = 240
local realpartsMinZ = -240

local realpartsy = 2
local realpartsx = math.random(realpartsMinX, realpartsMaxX)
local realpartsz = math.random(realpartsMinZ, realpartsMaxZ)

wait(7)

if #game.Players:GetPlayers() >= 2 then -- checking if player count is 2 then executing code
	local realpart = Instance.new("Part")
	realpart.Parent = game.Workspace.parts
	realpart.Name = "RealPart"
	realpart.CFrame = CFrame.new(realpartsx,realpartsy,realpartsz)
	game.Workspace.parts.RealPart.Touched:Connect(function(hit)
		if game.Players:GetPlayerFromCharacter(hit.Parent) then
			print("RealPart touched by " ..hit.Parent.Name)
			game.Workspace.parts.RealPart:Destroy()
		end
	end)
end
The script that kicks all players
wait(8) -- don't mind all the waits
game.Workspace.parts.RealPart.Touched:Connect(function(hit) -- if the part gets touched
	if game.Players:GetPlayerFromCharacter(hit.Parent) then -- verifying that it's a player
		print("Success")
		wait(5)
		for i,v in pairs(game.Players:GetPlayers()) do -- getting all players to kick them
			v:Kick("test") -- kicking them
		end
	end
end)
2 Likes

why do you need the 3rd script, if the second script is a server script (which it is) you could just kick the players from there… also, please let us know if you get any errors and what prints in the real game, also, you do know that to test it in the real game you need 2 or more players…

To answer your first question it’s for other reasons that I don’t want to talk about as it is not related to this topic but it has a reason to be a 3rd script also I know I test the game in testing mode as 2 players but I still don’t get any errors, I know this is very weird.

what prints and what doesn’t? You have to make sure that all scripts run properly.

  1. No I am not in team create
  2. I can provide you a GIF of what is happening, here it is:

Everything prints just fine the issue is that the scripts which creates the parts and kick the players don’t work.

All my scripts are in ServerScriptService by the way.

the while loop will pretty much run forever (until one of the players leaves) so I don’t see as to why it doesn’t spawn the parts, did you check the explorer tab and make sure again??

Yes I will give you a GIF for proof:

As you can see the script which repeatedly creates the parts are marked with green meaning that they are working but at the same time they are not and I don’t know why, don’t mind the other scripts by the way.

Your second code won’t run after 7 seconds if there are less than 2 players. If you are trying to make the code run after there are two players, you can do a loop until the appropriate amount of players have joined.

local realpartsMaxX = 240 -- to spawn parts around the map randomly
local realpartsMinX = -240
local realpartsMaxZ = 240
local realpartsMinZ = -240

local realpartsy = 2
local realpartsx = math.random(realpartsMinX, realpartsMaxX)
local realpartsz = math.random(realpartsMinZ, realpartsMaxZ)

wait(7)

repeat -- wait until 2 or more players
	wait()
until
	#game.Players:GetPlayers() >=2

local realpart = Instance.new("Part")
realpart.Parent = game.Workspace.parts
realpart.Name = "RealPart"
realpart.CFrame = CFrame.new(realpartsx,realpartsy,realpartsz)
game.Workspace.parts.RealPart.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		print("RealPart touched by " ..hit.Parent.Name)
		game.Workspace.parts.RealPart:Destroy()
	end
end)

You can get rid of the third script and combine it with your second script, since you created “RealPart” in said script. It would end up looking like this:

local realpartsMaxX = 240 -- to spawn parts around the map randomly
local realpartsMinX = -240
local realpartsMaxZ = 240
local realpartsMinZ = -240

local realpartsy = 2
local realpartsx = math.random(realpartsMinX, realpartsMaxX)
local realpartsz = math.random(realpartsMinZ, realpartsMaxZ)

wait(7)

repeat -- wait until 2 or more players
	wait()
until
	#game.Players:GetPlayers() >=2

local realpart = Instance.new("Part")
realpart.Parent = game.Workspace.parts
realpart.Name = "RealPart"
realpart.CFrame = CFrame.new(realpartsx,realpartsy,realpartsz)

realpart.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		print("RealPart touched by " ..hit.Parent.Name)
		game.Workspace.parts.RealPart:Destroy()
	end
end)

realpart.Touched:Connect(function(hit) -- if the part gets touched
	if game.Players:GetPlayerFromCharacter(hit.Parent) then -- verifying that it's a player
		print("Success")
		wait(5)
		for i,v in pairs(game.Players:GetPlayers()) do -- getting all players to kick them
			v:Kick("test") -- kicking them
		end
	end
end)

Though I still don’t understand why you need two different touched functions? Couldn’t you combine them into one?

2 Likes

Ok you actually somehow fixed the issue with RealPart and kicking all player scripts not working, your a legend but now how do we fix the script which randomly spawns parts around the map? a.k.a “FakePart”

But I honestly still don’t get how you fixed it… lol it’s basically the same thing.

When your script runs, it checks if there are 2 or more players in the server. If there isn’t, then the script will not run at all. To fix that, I added a loop that repeats until there are 2 or more players:

repeat -- wait until 2 or more players
	wait()
until
	#game.Players:GetPlayers() >=2

The same goes for your first script, once it sees that there isn’t 2 or more players, the while loop no longer runs. If I had to guess, the loop is called before you have 2 players in your game and it ends up never running. You can run a check in the loop itself to see if there are 2 or more players.

while wait(1) do -- checking if the player count is 2 then executing the code by repeatedly spawning parts
	if #game.Players:GetPlayers() < 2 then return end -- needs 2 or more to proceed
	local fakepartsx = math.random(fakepartsMinX, fakepartsMaxX)
	local fakepartsz = math.random(fakepartsMinZ, fakepartsMaxZ)
	local fakeparts = Instance.new("Part")
	fakeparts.Name = "FakePart"
	fakeparts.Parent = game.Workspace.parts
	fakeparts.Anchored = false
	fakeparts.CFrame = CFrame.new(fakepartsx,fakepartsy,fakepartsz)
end
1 Like

Lmao you fixed all the scripts but if #game.Players:GetPlayers() < 2 then return end didn’t work

Apparently what worked is repeat – wait until 2 or more players
wait()
until
#game.Players:GetPlayers() >=2

Thanks once again your a legend 681766487492657153

1 Like

That’s because the first piece of code you listed terminates the script as soon as it executes, where as the second one repeats until the conditions are met.