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

Why wont my tool work after I get it with a script but works when its in StarterPack?

Hello, I made a complicated gun tool, so posting the entire script here will give you a headache.

My issue is that whenever I get the tool via script, the tool wont play the idle animation or do the actions the gun supposed to do. I tried to check if the script even detects whenever I equip it - it doesn't.

local plr = game.Players.LocalPlayer
local char = plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local gun = plr.Backpack:WaitForChild("M1A1") --gun name
local animations = gun.Animations
local idleTrack = hum:LoadAnimation(animations.Idle)

gunEquipped = false

--Functions
function equip()
	if not gunEquipped  then
		idleTrack:Play()
		gunEquipped = true
		uis.MouseIconEnabled = false
	end
end

function unequip()
	if gunEquipped then
		idleTrack:Stop()
		gunEquipped = false
		uis.MouseIconEnabled = true
	end
end

--Events
gun.Equipped:Connect(function()
	equip()
end)

gun.Unequipped:Connect(function()
	unequip()
end)

The script above would work only if the gun was in StarterPack when I play the game, but it I try to parent it manually to the Backpack (via script or picking the gun up) the gun just wont work, its scripts dont work, I just hold the gun model and thats it.

Did anyone encounter this issue? How do I solve this?

2 comments

0

Are there any errors? Can you show the rest of the script, such as where gun is defined?

0

No there are no errors, whenever I get the tool with a script (I want to make a loadout system that gives you the gun) the tool just wont work. I just hold it but no animations, no actions, nothing. I edited my question thats a more detailed script

1 answer

Use WaitForChild()

WaitForChild() basically waits until for a specific child to be parented to a parent.

https://developer.roblox.com/en-us/api-reference/function/Instance/WaitForChild

Heres an example.

this script will wait for the child to be parented into the Backpack once the child has been partned to the backpack it will print out "yes"

local tool = game.Players.LocalPlayer.Backpack:WaitForChild("LinkedSword")

print("yes")

Hope this helps

5 comments

0

When I parent it manually (with script, pick it up..) the tool doesnt work.. but when I put it inside StarterPack and spawn with it, the tool works.

0

Dude, I've said use waitforchild()

0

I've misunderstood sorry, I used this " local gun = plr.Backpack:WaitForChild("M1A1") " but it doesn't work either, same result

0

That's odd, It's probably your own code that is the problem.

0

edit- fixed url

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