Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

0
thank you so much! jesusbeef 33 — 4y
Ad
Log in to vote
-1
Answered by 4 years ago
Edited 4 years ago

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

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. ForeverBrown 356 — 4y
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 jesusbeef 33 — 4y

Answer this question