Why Isn't This Script Working?

this is a script in a part in workspace:

script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
if plr.leaderstats.Rebirths.Value >= 5 then
script.Parent.CanTouch = true
end
end)

2 Likes

You should probably post the full script. As of this moment, you said the script is in a part in a workspace. But you’re not even using the hit argument.

But then you’re using script.Parent.Parent to get the player? If the script is indeed in a part in a workspace. Then script.Parent is the part, and script.Parent.Parent is the workspace. Therefore plr is nil. Are you setting the part’s CanTouch property to false anywhere? If not why are you setting it to true?

1 Like

im trying to make when a player touches a part and has over 5 rebirths then cantouch = true

You were trying to get a player from the game itself, not the player that touched the part.

script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr.leaderstats.Rebirths.Value >= 5 then
script.Parent.CanTouch = true
end
end)

I don’t you what are you trying to do, set CanTouch to true? okay, but this is the fixed one

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
        if plr and plr.leaderstats.Rebirths.Value >= 5 then
            script.Parent.CanTouch = true
        end
    end
end)
1 Like

Is CanTouch originally false? If so, how will the Touch event be fired if CanTouch isn’t enabled? Are you trying to enable CanTouch for the player’s character if they have atleast 5 rebirths?

Therefore the Touched event on the part would only fire if the player have atleast 5 rebirths?

3 Likes

The part needs to have its “CanTouch” property enabled for its “.Touched” event to fire.

2 Likes

cantouch is originaly false and i wanted cantouch to be true if the player who touches the part has over 5 rebirths

But why tho? Why can’t you just check if the player has 5 rebirths upon touch and then execute the code needed.

1 Like

When the part is touched, hit is defined as the object that touched the part.

script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr.leaderstats.Rebirths.Value >= 5 then
script.Parent.CanTouch = true
end
end)

Secondly, CanTouch needs to be enabled for the Touched event to fire. If you want the player to be able to interact with the part, make the script set CanCollide to true instead of CanTouch

1 Like
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr.leaderstats.Rebirths.Value >= 5 then
script.parent.CanTouch = true
end
end)

Make sure the part has the CanTouch option set to true so the part can actually be touched.