Script doesn't work on a simple code

I’m trying to achieve a simple touch function where touching a part gives you a string value,
the problem is that it just doesn’t follow.

script.Parent.Touched:Connect(function(part)

script.Parent.Parent.Part.BillboardGui.TextBox.Text = "what"

local hasOil = Instance.new("ObjectValue")

hasOil.Name = "touchedOil"

hasOil.Parent = game.Players:GetPlayerFromCharacter(part.Parent)

hasOil.Value = game.Players.LocalPlayer.UserId

--game.ReplicatedStorage.oiltouched:FireServer()

end)

I don’t get why it just doesn’t follow.

1 Like

What does it not do and what does it do?

Tip: Use more variables, instead of using endless .Parent.Parent.Parent.Parent etc.

its cause the touching parts parent might not be a character

script.Parent.Touched:Connect(function(part)

local player = game.Players:GetPlayerFromCharacter(part.Parent) -- get the player
if player then -- check if the player exists
-- rest of the script
end

end)

Also if its in a local script then it has to be a child of either the player or the character to run I think

local part = script.Parent

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			script.Parent.Parent.Part.BillboardGui.TextBox.Text = "what"
			local hasOil = Instance.new("IntValue")
			hasOil.Name = "touchedOil"
			hasOil.Parent = player
			hasOil.Value = player.UserId
			--game.ReplicatedStorage.oiltouched:FireServer()
		end
	end
end)

game.Players.LocalPlayer is not valid in server scripts, you can get the player instance which touched the part by doing what I did in the above additionally you were attempting to assign an integer value (user ID) to an ObjectValue instance which wouldn’t work, I’ve switched that for an IntValue instance.

It’s a local script, I forgot to mention that!

Tried that, didn’t work sadly.

What it doesn’t do is everything, what it’s supposed to do is make a StringValue and parent it to the player.
I thought of also using variables but I have to only use it once.

local part = script.Parent
local player = game.Players.LocalPlayer

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		script.Parent.Parent.Part.BillboardGui.TextBox.Text = "what"
		local hasOil = Instance.new("IntValue")
		hasOil.Name = "touchedOil"
		hasOil.Parent = player
		hasOil.Value = player.UserId
		--game.ReplicatedStorage.oiltouched:FireServer()
	end
end)

This will work as a local script, on an additional note you mention you wanted to use a StringValue instance but used an ObjectValue instance instead.

local hasOil = Instance.new("StringValue")
hasOil.Value = tostring(player.UserId)

You can use the above instead of an IntValue instance if necessary.

Does the script run? If not can you tell me the parent of it and also put a print at the first line to see if it works

It didn’t print anything when I added the print function at line 1

For the parent.
image

Idk if this works, but try creating the value from the server i.e the script tht runs when the remote event is fired.

It worked, Thanks for the help! :happy2: