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

How would I compare a player.UserId to an StringValue?

I'm making a script for player ownership where only the player who owns an object can interact with it. Here's my script:

UIS.InputChanged:Connect(function(input)
	local player = game.Players.LocalPlayer
	local openInfo = player:WaitForChild("PlayerGui"):WaitForChild("E_to_open_box")
	local ownerGui = player:WaitForChild("PlayerGui"):WaitForChild("OwnerGui"):WaitForChild("Frame")
	local mouse = player:GetMouse()
	local target = mouse.Target
	if target then
		if target:FindFirstChild("Box") then
			if target.PlayerOwner.Value == player.UserId then
				local distance = player:DistanceFromCharacter(target.Position)
				if distance <= 30 then
					openInfo.Enabled = true
					openInfo.Adornee = target
					ownerGui.Visible = false
				end
			else
				ownerGui.PlayerOwner.Text = target.PlayerName.Value
				ownerGui.Visible = true
			end
		else
			openInfo.Enabled = false
			openInfo.Adornee = nil
			ownerGui.Visible = false
		end
	end
end)

PlayerOwner is a StringValue, should I change that to an IntValue? When I print both the PlayerOwner.Value and the player.UserId, they are the same, yet the if target.PlayerOwner.Value == player.UserId then isn't working.

5 comments

0

Forgot to mention but this is also a LocalScript inside StarterPlayerScripts.

0

Use an ObjectValue and change the value of it to the player, then make sure ObjectValue.Value == player

0

That didn't work, I still got the same result.

0

if it's a string value, you can do tostring(player.UserId) -> which converts the players id to a stringvalue, so it can be compared

0

oSyrical that worked thanks!

1 answer

This maybe? If it's not this then I don't understand your question

if tostring(target.PlayerOwner.Value) == player.UserId

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