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

can this script be used to unanchor parts on player's touch?

this script i've used is supposed to unanchor parts on the player's touch, however it isn't working. what is written wrong in this code?

game.Players.PlayerAdded:Connect(function(player)
	
	
	local hp = script.Parent.HatchPiece1 and script.Parent.HatchPiece2 and script.Parent.HatchPiece3
	
	hp.Touched:Connect(function(touched)
		if player == touched then
			hp.Anchored = false
		end
	end)
		
end)

2 answers

✓ Accepted answer

I'm thinking your problem is..."player" is the player stored in game.Players and touched is the part that touched the detector (the players foot). You need detect if the part that is touching the detector is the players character. You then need to get the player instance from the players character(if you use that to compare).

You can do this like so...

game.Players.PlayerAdded:Connect(function(player)
	script.Parent.Touched:Connect(function(hit)
		local playerhit = game.Players:GetPlayerFromCharacter(hit.Parent)
		if playerhit then
			print("Hi there")
		end
	end)
end)

Sorry, not the greatest at explaining things so here's some documentation: https://developer.roblox.com/en-us/api-reference/function/Players/GetPlayerFromCharacter

Edit: You also need to change your variable set up. Doing it like this

local hp = script.Parent.Part1 and script.Parent.Part2 and script.Parent.Part3

will only change properties for part3

I recommend setting it up like this

local hp,bh,tp = script.Parent.Part1,script.Parent.Part2,script.Parent.Part3

or iterate through a model

local model = script.Parent

for i,v in pairs(model)do
	if  v:IsA("Part")then
		v.Anchored = false
	end
end

Please do upvote if this helped ;)

1 comment

0

thank you so much!

ok look the script in in > HatchPiece3 if its in hatchPiece3, its cant get hatchpiece 2 by script.Parent you would probably do this >


game.Players.PlayerAdded:Connect(function(player)
local hp = script.Parent.HatchPiece1 and script.Parent.Parent.HatchPiece2 and script.Parent.Parent.HatchPiece3

hp.Touched:Connect(function(touched)
if player == touched then
hp.Anchored = false
end
end)

end)

and put all these parts to the model

2 comments

0

if you use variables like that then you would need to either do it like this "local hp,bh,tp = script.Parent.Part1,script.Parent.Part2,script.Parent.Part3" or you can iterate through a model.

0

hatchpieces are all grouped up in Hatch. Hatch is parent of the script, so this doesn't work. the parenting isn't the issue, the issue lies elsewhere however i can't find the solution

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