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

Turn into character script does not work, can anyone explain me what is wrong with the script?

Asked by 8 years ago
Edited 8 years ago

So, I'm working on a game where you can turn into a prop as soon as you click a part in workspace. The problem is that it does work until you become the prop, after that you can't click the parts again and you get errors if you click somewhere (even if you click the sky). Can anyone help me with this?

Localscript:

local events = game.ReplicatedStorage.Events
local colorevent = events.ChangeColor
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local gui = Player.PlayerGui
local mouseM = gui.HUD.MouseMessage
local props = game.ReplicatedStorage.Props

colorevent.OnClientEvent:connect(function(Color)

end)

Mouse.Move:connect(function()
    local X, Y = Mouse.X, Mouse.Y
    if Mouse.Target ~= nil then
    local a = Mouse.Target.Position
    if Mouse.Target.Parent:FindFirstChild("IsProp") and
        X > gui.HUD.AbsolutePosition.x and
        X > gui.HUD.AbsolutePosition.x and
        X < gui.HUD.AbsolutePosition.x+gui.HUD.AbsoluteSize.x and
        Y < gui.HUD.AbsolutePosition.y+gui.HUD.AbsoluteSize.y 
        then
    if Player:DistanceFromCharacter(a) < 18 then
        mouseM.Visible = true;
        mouseM.Position = UDim2.new(0,X+1,0,Y+1);
        mouseM.Text = "Turn into a "..Mouse.Target.Parent.IsProp.Propname.Value;
        Mouse.Button1Down:connect(function()
            local b = Mouse.Target.Parent.IsProp.Propname.Value
            events.SpawnCharacters:FireServer("unfinished",b)
        end)
    else
        mouseM.Visible = false;
        mouseM.Text = ""
    end
    else
        mouseM.Visible = false;
        mouseM.Text = ""
    end
    end
end)

This is the error:

16:29:20.864 - Players.Player1.PlayerScripts.Framework:28: attempt to index field 'Target' (a nil value)
16:29:20.865 - Stack Begin
16:29:20.866 - Script 'Players.Player1.PlayerScripts.Framework', Line 28
16:29:20.867 - Stack End

1 answer

Log in to vote
2
Answered by 8 years ago

Your problem is that once the MouseButton1Down function once fired will last until death, not just until one click, so if the player within that time clicks the air, the Target is nil giving you the error displayed. Here's the fixed code:

Mouse.Move:connect(function()
    local X, Y = Mouse.X, Mouse.Y
    if Mouse.Target ~= nil then
        local a = Mouse.Target.Position
        if Mouse.Target.Parent:FindFirstChild("IsProp") and X > gui.HUD.AbsolutePosition.x and X > gui.HUD.AbsolutePosition.x and X < gui.HUD.AbsolutePosition.x+gui.HUD.AbsoluteSize.x and Y < gui.HUD.AbsolutePosition.y+gui.HUD.AbsoluteSize.y then
            if Player:DistanceFromCharacter(a) < 18 then
                mouseM.Visible = true; --from here
                mouseM.Position = UDim2.new(0,X+1,0,Y+1); 
                mouseM.Text = "Turn into a "..Mouse.Target.Parent.IsProp.Propname.Value; --till here
                Mouse.Button1Down:connect(function()
                    if Target ~= nil then
                        local b = Mouse.Target.Parent.IsProp.Propname.Value
                        events.SpawnCharacters:FireServer("unfinished",b)
                    end
                end)
            else
                mouseM.Visible = false;
                mouseM.Text = ""
            end
        else
            mouseM.Visible = false;
            mouseM.Text = ""
        end
    end
end)
Ad

Answer this question