Touched returns the part that touched it. You need to figure out which character touched it by finding who owns the bodypart that touched your tycoon door, then use that to find the player.
TycoonDoor.Touched:Connect(function(part)
if PlayerOwner.Value then-- Abort code if a player already owns this tycoon
return
end
if not part.Parent:FindFirstChild("Humanoid") then-- This part isn't owned by a character-- abort code
return
end
local TycoonOwner = PlayerService:GetPlayerFromCharacter(part.Parent)
if not TycoonOwner then-- Character is not owned by a player (probably an NPC), abort code
return
end
PlayerOwner.Value = TycoonOwner
print("worked")
end)
local touchedPlayer = TycoonOwner.Parent:FindFirstChild("Humanoid") and PlayerService:GetPlayerFromCharacter(TycoonOwnner.Parent.Parent)
if not touchedPlayer then return end
PlayerOwner.Value = touchedPlayer.Name
I missed that PlayerOwner is a stringvalue and not an objectvalue. Try this instead.
TycoonDoor = script.Parent
PlayerService = game:GetService("Players")
PlayerOwner = Instance.new("StringValue")
PlayerOwner.Parent = TycoonDoor
TycoonDoor.Touched:Connect(function(part)
if PlayerOwner.Value ~= "" then-- Abort code if a player already owns this tycoon
return
end
if not part.Parent:FindFirstChild("Humanoid") then-- This part isn't owned by a character-- abort code
return
end
local TycoonOwner = PlayerService:GetPlayerFromCharacter(part.Parent)
if not TycoonOwner then-- Character is not owned by a player (probably an NPC), abort code
return
end
PlayerOwner.Value = TycoonOwner.Name
end)
i changed it back to stringvalue because i was looking around the forums that a string value can not reference or access a 3d object, then i realized that was false so i changed back to stringvalue and removed objectvalue
You can if you want, it just makes breaking up comments worse and makes it harder to read long text in the devforum’s little codebox thingy
Edit: Also you probably just want to use an object value for this use case. ObjectValues just directly point to the player and can be used in if checks all the same. In fact you can get the same result as a string value simply by doing PlayerOwner.Value.Name.
There’s only one if case in here while the example code uses 3? Doesn’t seem like you’re using these compound if checks correctly. I’ll quickly make it all one line to show you how it should be looking
Edit:
TycoonDoor = script.Parent
PlayerService = game:GetService("Players")
PlayerOwner = Instance.new("ObjectValue")
PlayerOwner.Parent = TycoonDoor
TycoonDoor.Touched:Connect(function(part)
if PlayerOwner.Value or not part.Parent:FindFirstChild("Humanoid") then
-- Check #1: Abort code if a player already owns this tycoon
-- Check #2: Abort code if it isn't owned by a character
return
end
local TycoonOwner = PlayerService:GetPlayerFromCharacter(part.Parent)-- We need a reference to this value after the if check, hense why we're not getting it in the if check above us.
if not TycoonOwner then-- Character is not owned by a player (probably an NPC), abort code
return
end
PlayerOwner.Value = TycoonOwner
print("Worked")
end)
Again this is using objectvalue rather than stringvalue because objectvalues in this use case are more versatile than using a stringvalue.
You’re gonna get errors without the other if checks depending on what touches your tycoon door & also without the first if check other players can steal your tycoon by just touching the tycoon door again. Not sure if that’s intended behavior.