How do I make it so it works when any player is out of the room?

This is hard to explain without any sort of video so I’m going to include 2 videos for demonstration.

Basically, how this script works is that its activated by two scripts, one is a timer script which is shown above. And a shock collar command that attaches a shock collar to a player. Both are serversided scripts and are made using the Adonis API. The shock collar script adds to a modulescript table which contains names of the users who are equipped with a shock collar. The timer script activates the shock event once it hits 0.

The problem here is that when the script inserts a bool value in the player’s models, It only goes off of one player’s bool value and fires the script when that player is outside of the safe zone. I want the function to start no matter which player is out of the safe zone. Any help is appreciated.

Video 1

Video 2

Script

game.Players.PlayerAdded:Connect(function(plr)
	wait(1)
	local shockremote = game.ReplicatedStorage.Shock
	local shocksound = game.ReplicatedStorage.PlayShock
	local touchbool = Instance.new("BoolValue")
	local isTouching = touchbool
	local moduletable = require(game.ServerScriptService.PlayerList)
	local playertable = moduletable
	
	touchbool.Name = "isTouching"
	touchbool.Value = false
	touchbool.Parent = plr.Character
	
	
	workspace.Layers.ShockSafeZone.Touched:Connect(function()
		isTouching.Value = true
	end)
	
	workspace.Layers.ShockSafeZone.TouchEnded:Connect(function()
		isTouching.Value = false
	end)
	
		shockremote.OnServerEvent:Connect(function()
			for i in pairs(playertable) do
			if plr.Name == playertable[i] then
				if isTouching.Value == false then
						local humanoid = plr.Character:WaitForChild("Humanoid")
						humanoid.PlatformStand = true
						humanoid.Health = humanoid.Health - 40
						shocksound:FireClient(plr)
						wait(1)
						humanoid.PlatformStand = false
					end
				end	
			end
		end)	
end)
2 Likes

I don’t quite understand what you mean. In the first video it appears as if nothing happens to either player. Is the person in the hall supposed to be shocked? In the second video, both of you are shocked when you are outside it. Do you mean that each collar should be handled independently from each other, meaning that all players who are outside the safe-zone will be shocked?

Or do you mean that all players are shocked if at least one person is outside the safe-zone?

The person in the hall is supposed to be shocked, basically the problem is that only the players in the hall are supposed to be shocked but for some reason it requires me to be in the hall so they get shocked.

TLDR:
The player in the other room is safe, and the one outside isn’t safe.

Alright. Thank you for the clarification! Something I find somewhat weird the shochremote.OnServerEvent. You have it inside the .PlayerAdded which means that for each player the remote event will fire, which in turn leads to all players running through the playerlist to see if each player should be shocked. I would recommend moving outside to be it’s “separate” thing.

Are you certain that the playertable contains the correct information no matter if all players are outside or not?

1 Like

Hope this helps.

1 Like

I am currently AFK at the moment, but I’ll test this to see if it works, thank you.

1 Like

The playertable doesn’t go by if the player is outside or not, it contains each player that is wearing a shockcollar.

Are you sure this works correctly? I see no issue with the code within your remote-event, which means the issue likely lies within the way the “should be shocked” value is assigned.

Yeah, when I tested it I made sure to check the BoolValues stored in the characters work. They did end up working but I don’t think it’s a problem with the remote events. It might be more of the way I decided to store the indicators of which players are outside or inside. Maybe I should try using tables?

Tables are usually a good solution. Also, consider using Instance Attributes (roblox.com) rather than (ex) BoolValue. I believe I read somewhere that they were 10’s if not 100’s of times faster in some areas compared to values, while remaining the same in other areas.

I tried this by using a for loop to go through the children of Players and no errors pop up.

for i = 1, #playergetch do
	local playername = playergetch[i].Name
	if getTouch(workspace.playername,workspace.Layers.ShockSafeZone) == true then
		print('druh')
	end
end

I reccomend you do something like this…

function getParams(char,parameterpart)
	if table.find(workspace:GetPartsInPart(parameterpart),char.PrimaryPart) then
		print(Char.Name.." found in "..parameterpart.Name)
		return true
	end
end

local part = boxpart

for _,player in pairs(game.Players:GetChildren)) do
   if player:IsA("Player") then
      local character = player.Character
      if getParams(character,part) == true then
            -- does what i want the script to do here
      end
   end
end

I’m still having a problem, nothing is coming up in the console, I slightly modified it but not too much.

function getParams(char,parameterpart)
	if table.find(workspace:GetPartsInPart(parameterpart),char.PrimaryPart) then
		print(Char.Name.." found in "..parameterpart.Name)
		return true
	end
end

local part = boxpart

for _,player in pairs(game.Players:GetChildren)) do
   if player:IsA("Player") then
      local character = player.Character
      if getParams(character,part) == true then
            -- does what i want the script to do here
      end
   end
end

This is se exact code I gave you, can you show me your script and your modifications?

My bad, here’s the script.

function getParams(char,parameterpart)
	if table.find(workspace:GetPartsInPart(parameterpart),char.PrimaryPart) then
		print(char.Name.." found in "..parameterpart.Name)
		return true
	end
end

local part = game.Workspace.Layers.ShockSafeZone

for _,player in pairs(game.Players:GetChildren()) do
	if player:IsA("Player") then
		local character = player.Character
		if getParams(character,part) == true then
			print('test')
		end
	end
end

Hm… And you’re sure this is a server script etc, Your problem could not be scripting but something smaller and silly.

Yeah I’ve made sure that it’s in a server script. Still can’t really identify the problem.

I will try making a mockup when I have the time. :slight_smile: