Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does my touched event work only if a certain person does it?

I'm making something where you claim an area if you walk up to it, and then nobody can walk into yours and you can't walk into anyone else's

In Studio it works perfectly, but when I go into the game with my alt, things get confusing

The touched events seem to only trigger when I do it, when my alt does it, literally nothing happens, and on top of that, it happens for everyone, not just me

What?

Code (ServerScript in ServerScriptService):

local Nil = game.Players:WaitForChild("Nil")
local Owner1 = Instance.new("StringValue")
local Owner2 = Instance.new("StringValue")

game.Players.PlayerAdded:Connect(function(player)
Nil.Parent = player
	player.Nil.Value = true
end)
	
game.Workspace.Slot1.Ownership.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if Owner1.Value == "" and Player.Nil.Value == true then
			Owner1.Value = Player.Name
			Player.Nil.Value = false
			game.Workspace.Slot1.GUI.BillboardGui.TextLabel.Visible = false
		else
			return nil
		end
		if Player.Name == Owner1.Value then
			game.Workspace.Slot1.Ownership.CanCollide = false
		end
	end
end)

game.Workspace.Slot2.Ownership.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if Owner2.Value == "" and Player.Nil.Value == true then
				Owner2.Value = Player.Name
				Player.Nil.Value = false
			game.Workspace.Slot2.GUI.BillboardGui.TextLabel.Visible = false
		else
			return nil
		end
		if Player.Name == Owner2.Value then
			game.Workspace.Slot2.Ownership.CanCollide = false
		end
		end
end)

1 answer

You're parenting the BoolValue "Nil" under the first player that joins. Only the first player joining will have "Nil" as a child. To fix that, you'll clone "Nil" and parent it to each player that joins. That way everyone gets "Nil"!

Edit: Didn't see a need for Owner1 and Owner2 to be StringValues; converted them to variables

Updated code:

local Nil = game.Players:WaitForChild("Nil"):Clone() -- now we're cloning "Nil" instead of setting the original "Nil" to the variable
local Owner1 = nil
local Owner2 = nil

game.Players.PlayerAdded:Connect(function(player)
	player.Nil.Value = true
	Nil.Parent = player
end)

game.Workspace.Slot1.Ownership.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if Owner1 == nil and Player.Nil.Value == true then
			Owner1 = Player.Name
			Player.Nil.Value = false
			game.Workspace.Slot1.GUI.BillboardGui.TextLabel.Visible = false
		else
			return nil
		end
		if Owner1 == Player.Name then
			game.Workspace.Slot1.Ownership.CanCollide = false
		end
	end
end)

game.Workspace.Slot2.Ownership.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if Owner2 == nil and Player.Nil.Value == true then
			Owner2 = Player.Name
			Player.Nil.Value = false
			game.Workspace.Slot2.GUI.BillboardGui.TextLabel.Visible = false
		else
			return nil
		end
		if Owner2 == Player.Name then
			game.Workspace.Slot2.Ownership.CanCollide = false
		end
	end
end)

2 comments

0

now it just lets you walk into any claimed land

0

Updated, try that. I didn't see a need for new StringValue instances, so I converted them to variables.

This discussion is preserved as it was. New answers, comments, and votes are closed.