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

Code working on Studio, but not on server. Using LocalScripts for this case is wrong?

Errors: Handle is not a member of tool (on the main script.) and Character is a nil value. (following script.)

Selection script: (localscript in playergui)

--[Locals]--
 
local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse() --Getting the player's mouse
local folders = workspace.OreBlocks
local tool = script.Parent.Parent.Backpack:WaitForChild("Pickaxe")
local active = false
local humanoid = character.Humanoid

local selection = Instance.new("SelectionBox")
selection.Color3 = Color3.new(255,255,255)
selection.Parent = player.PlayerGui
selection.Name = ("PickaxeSelection")
selection.LineThickness = 0.09

--[Logic]--
 
mouse.Move:connect(function()
    if active and humanoid.Health == 100 then
        local target = mouse.Target
    
        if not target then 
            selection.Adornee = nil
        elseif target.Parent == folders then
            selection.Adornee = target
        else
            selection.Adornee = nil
        end 
    end

end)

tool.Equipped:connect(function()
    active = true
end)

tool.Unequipped:connect(function()
    active = false
	selection.Adornee = nil
end)

Main script: (localscript in my pickaxe tool)

--[Made by TheBoxofGhosts]--

--[Locals]--

local mouse = game.Players.LocalPlayer:GetMouse()
local tool = script.Parent
local sound = tool.Handle:WaitForChild("Sound")
local oresound = tool.Handle:WaitForChild("OreSound")
local selection = game.Players.LocalPlayer.PlayerGui:WaitForChild("PickaxeSelection")

--[Logic]--

local function onMining()
	if mouse.Target and mouse.Target.Name == ("DirtBlock") then
		sound:Play()
		script.Disabled = true
		selection.Color3 = Color3.new(0, 255, 0)
		mouse.Target:Destroy()
		wait(0.1)
		selection.Color3 = Color3.new(255, 255, 255)
		wait(0.2)
		script.Disabled = false
	end
end

tool.Activated:connect(onMining)

local function onMiningOre()
	if mouse.Target and mouse.Target.Name == ("CopperBlock") or mouse.Target and mouse.Target.Name == ("IronBlock") then
		oresound:Play()
		script.Disabled = true
		selection.Color3 = Color3.new(0, 255, 0)
		mouse.Target:Destroy()
		wait(0.1)
		selection.Color3 = Color3.new(255, 255, 255)
		wait(0.2)
		script.Disabled = false
	end
end

tool.Activated:connect(onMiningOre)

1 answer

✓ Accepted answer

Hi TheBoxOfGhosts!

In both cases, the errors happen because you're not checking to make sure that the items in question exist. It is very important that you ensure something exists before you try modifying it. The following are corrections for your scripts:

PlayerGui script:

local player = game.Players.LocalPlayer

-- Wait until the Character exists. Can't use WaitForChild in this case!
repeat
    wait(.1)
until
    player.Character

local character = player.Character
local mouse = player:GetMouse() --Getting the player's mouse
local folders = workspace:WaitForChild("OreBlocks")
local tool = script.Parent.Parent.Backpack:WaitForChild("Pickaxe")
local active = false

-- In this case, we can use WaitForChild, because it's a method of character. ;)
local humanoid = character:WaitForChild("Humanoid")

-- ...

Axe Script:

local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local sound = handle:WaitForChild("Sound")
local oresound = handle:WaitForChild("OreSound")

Now, I must warn you: These are very simple mistakes that far too often turn into Scripting Helpers questions. You must be more vigilant and try your best to determine the cause to these problems on your own. Please be more careful next time.

Have a nice day, TheBoxOfGhosts, and best of luck with your mining game!

Cheers,

tkcmdr

3 comments

1

it's better to use: local character = player.Character or player.CharacterAdded:Wait() instead of a repeat until loop

0

Thank you! :D

0

Great idea, MBacon15! Thanks for pointing that out!

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