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