Im trying to make a gun system but apparently WaitForChild Returns a string.
I have no clue whats causing this.
Game Folder:
Script:
-- Variables
local uis = game:GetService("UserInputService")
local weapon = game:GetService("ReplicatedStorage"):WaitForChild("Weapons")
local plr = game:GetService("Players").LocalPlayer
local weapons = plr:WaitForChild("Weapons")
local cam = game:GetService("Workspace").CurrentCamera
local equipRemote = game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("Equip")
local viewmodel;
local cs = game:GetService("ContextActionService")
-- Logic
function reload(weapon)
weapon:WaitForChild(weapons:WaitForChild("Primary").Value):WaitForChild("Animations"):WaitForChild("Deploy")
end
cs:BindAction("Reload", reload, true, Enum.KeyCode.R, Enum.KeyCode.ButtonX)
uis.InputBegan:Connect(function(input, gpe)
if not gpe and input.KeyCode == Enum.KeyCode.Q then
local ben = weapon:WaitForChild(weapons:WaitForChild("Primary").Value)
viewmodel = ben:WaitForChild("viewmodel"):Clone()
viewmodel.Parent = cam
viewmodel:WaitForChild("Animation"):WaitForChild("Animator"):LoadAnimation(ben:WaitForChild("Animations"):WaitForChild("Deploy")):Play()
end
end)
it errors in the reload function, with the error
ContextActionService: Unexpected error while invoking callback: Players.HeyIDoStuf.PlayerScripts.Gameplay.weaponClient:15: attempt to call missing method ‘WaitForChild’ of string
You are considering ‘weapon’ as an instance, when the first argument is actually the Action Name (the first string that is passed when calling BindAction).
Since you are attempting to bind the R key for reloading your weapon, the only thing you need in the reload function is to run the reloading process for the weapon (if that is what you are trying to achieve).
Here is your updated script:
-- Variables
local uis = game:GetService("UserInputService")
local weapon = game:GetService("ReplicatedStorage"):WaitForChild("Weapons")
local plr = game:GetService("Players").LocalPlayer
local weapons = plr:WaitForChild("Weapons")
local cam = game:GetService("Workspace").CurrentCamera
local equipRemote = game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("Equip")
local viewmodel;
local cs = game:GetService("ContextActionService")
-- Logic
local function reloadProcess()
--[[
Use this function to run the reloading process for the weapon
e.g. play reload sound and animation, update ammo count of the weapon etc.
]]
print("Reloading gun")
end
function reload(actionName, inputState, inputObj)
if actionName == "Reload" and inputState == Enum.UserInputState.Begin then
reloadProcess() -- runs the reloading function
end
end
cs:BindAction("Reload", reload, true, Enum.KeyCode.R, Enum.KeyCode.ButtonX)
uis.InputBegan:Connect(function(input, gpe)
if not gpe and input.KeyCode == Enum.KeyCode.Q then
local ben = weapon:WaitForChild(weapons:WaitForChild("Primary").Value)
viewmodel = ben:WaitForChild("viewmodel"):Clone()
viewmodel.Parent = cam
viewmodel:WaitForChild("Animation"):WaitForChild("Animator"):LoadAnimation(ben:WaitForChild("Animations"):WaitForChild("Deploy")):Play()
end
end)
1 Like