Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago
Edited 3 years ago

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):

01local Nil = game.Players:WaitForChild("Nil")
02local Owner1 = Instance.new("StringValue")
03local Owner2 = Instance.new("StringValue")
04 
05game.Players.PlayerAdded:Connect(function(player)
06Nil.Parent = player
07    player.Nil.Value = true
08end)
09 
10game.Workspace.Slot1.Ownership.Touched:Connect(function(hit)
11    if hit.Parent:FindFirstChild("Humanoid") then
12        local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
13        if Owner1.Value == "" and Player.Nil.Value == true then
14            Owner1.Value = Player.Name
15            Player.Nil.Value = false
View all 40 lines...

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

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:

01local Nil = game.Players:WaitForChild("Nil"):Clone() -- now we're cloning "Nil" instead of setting the original "Nil" to the variable
02local Owner1 = nil
03local Owner2 = nil
04 
05game.Players.PlayerAdded:Connect(function(player)
06    player.Nil.Value = true
07    Nil.Parent = player
08end)
09 
10game.Workspace.Slot1.Ownership.Touched:Connect(function(hit)
11    if hit.Parent:FindFirstChild("Humanoid") then
12        local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
13        if Owner1 == nil and Player.Nil.Value == true then
14            Owner1 = Player.Name
15            Player.Nil.Value = false
View all 40 lines...
0
now it just lets you walk into any claimed land SuperSM1 67 — 3y
0
Updated, try that. I didn't see a need for new StringValue instances, so I converted them to variables. efficacies 180 — 3y
Ad

Answer this question