So i'm trying to make a gun and this is giving me the error of "attempt to call field 'OnServerEvent' (a userdata value)"
any idea why?
function Pew()
local fireRate = script.Parent.FireRate.Value
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local bullet = ReplicatedStorage:WaitForChild("Bullet")
local damage = script.Parent.Damage.Value
local range = script.Parent.BulletSpeed.Value
local shooting = script.Parent.Shooting.Value
shooting = true
while wait(fireRate) and shooting == true do
script.Parent.Ammo.Value = script.Parent.Ammo.Value - 1
local newBullet = bullet:Clone()
newBullet.Position = script.Parent.ShootPos
newBullet.Parent = game.Workspace
newBullet.Velocity = script.Parent.ShootPos.CFrame.LookVector * range
newBullet.Touched:Connect(function(hit)
if hit:FindFirstChild("Humanoid") then
hit.Humanoid:TakeDamage(damage)
end
end)
end
function NoPew()
local fireRate = script.Parent.FireRate.Value
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local bullet = ReplicatedStorage:WaitForChild("Bullet")
local damage = script.Parent.Damage.Value
local range = script.Parent.BulletSpeed.Value
local shooting = script.Parent.Shooting
shooting = false
end
end
script.Shoot.OnServerEvent(Pew)
script.StopShoot.OnServerEvent(NoPew)

2 comments
What do you mean by Connect a function?
A listener connects an “event” which in other words is a function. Whenever the listener fires, we can manually create a raw function to be called, or connect an existing one, which is occurring above.