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

attempt to call field 'OnServerEvent' (a userdata value)?

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)

1 answer

OnServerEvent is an RBXScriptSignal. You need to Connect a function to build a listener.

script.Shoot.OnServerEvent:Connect(Pew)

The same will apply for NoPew. You also have a floating end within said function, which will raise an EOF exception. To fix that, simply remove it.

2 comments

0

What do you mean by Connect a function?

0

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.

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