Weapon System - Wrongly interpreting objects?

EDIT: Adjusted script in accordance to first reply.

(The script here is a bit odd, I’m simply trying to get basic client functionality. In the future, it’ll be rewritten better, and individual weapon stats and such will be in a module. It understandably does not work well with multiple players, but I’ll handle that later)

I’m working on the basis for a specialized weapon system, and I’m getting started with a simple sword made of baseparts. So far, it’s working great. The animations work, and timing is fine.

However, I’m starting to add damage functionality, and have had no luck. I’m trying to test some parts with print statements between client and server, and I’m getting some insanely incorrect responses.

Full localscript inside the weapon tool itself:

local localPlayer = game.Players.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()

local Tool = script.Parent
local animation = script.SwordEquip
local humanoid = character:WaitForChild("Humanoid")

local swordProp = character:WaitForChild("SwordProp") --Fake sword part on players hand, for animation purposes, irrelevant to issue
local swordDummy = character:WaitForChild("SwordDummy") --Fake sword part on players back, for animation purposes, irrelevant to issue
local debounce = false

local damageEvent = game.ReplicatedStorage:WaitForChild("Damage") --remoteevent


local Anims = {}
local connections = {}

local AnimIds = {
	"http://www.roblox.com/asset/?id=95678176300380", --equip anim
	"http://www.roblox.com/asset/?id=77433726648962" --attack anim
}

for i, ID in pairs(AnimIds) do
	animation.AnimationId = ID
	Anims[i] = humanoid:LoadAnimation(animation)
	local currentAnim = Anims[i]
	currentAnim.Priority = Enum.AnimationPriority.Action
end

--issue area starts

local hitConnection = nil

local function onHit(otherPart)
	if not otherPart or not otherPart:IsA("BasePart") then
		return
	end
	if otherPart:IsDescendantOf(Tool) then
		return
	end
	damageEvent:FireServer(
--The comments here are what these are SUPPOSED to return, in order.
        LocalPlayer,              --LocalPlayer
        Tool.Name,                -- weapon name (TestSword)
		10,                       -- damage amount (10)
		otherPart,                -- the part we actually hit (part of rig [limb, rootpart, etc.])
		2                         -- weapon debounce for the server side (2)
	)
end

local function enableTouch()
	if not hitConnection then
		hitConnection = Tool.Handle.Hitbox.Touched:Connect(onHit)
	end
end

local function disableTouch()
	if hitConnection then
		hitConnection:Disconnect()
		hitConnection = nil
	end
end

-- issue area ends

local function makeVisible() --for animation purposes
	Tool.Handle.Transparency = 0
	for i, v in Tool.Handle:GetChildren(_, true) do
		if v:IsA("BasePart") and v.Name ~= "Hitbox" then
			v.Transparency = 0
		end
	end
	
	swordProp.Transparency = 1
	for i, v in swordProp:GetChildren(_, true) do
		if v:IsA("BasePart") then
			v.Transparency = 1
		end
	end
	
	swordDummy.Transparency = 1
	for i, v in swordDummy:GetChildren(_, true) do
		if v:IsA("BasePart") then
			v.Transparency = 1
		end
	end
end

local function makeInvisible() -- for animation purposes
	Tool.Handle.Transparency = 1
	for i, v in Tool.Handle:GetChildren(_, true) do
		if v:IsA("BasePart") and v.Name ~= "Hitbox" then
			v.Transparency = 1
		end
	end
	
	swordProp.Transparency = 0
	for i, v in swordProp:GetChildren(_, true) do
		if v:IsA("BasePart") then
			v.Transparency = 0
		end
	end
end

local function OnEquip()
	--print("equipped")
	
	if connections.equip then
		connections.equip:Disconnect()
	end
	if connections.unequip then
		connections.unequip:Disconnect()
	end
		
	Anims[1]:Play()
	connections.equip = Anims[1]:GetMarkerReachedSignal("AttachWeapon"):Connect(function()
		makeVisible()
	end)
	
	enableTouch()
end

local function OnUnequip()
	--print("unequipped")
	
	for _, Anim in pairs(Anims) do
		if Anim.IsPlaying then
			Anim:Stop()
		end
	end
	
	if connections.equip then
		connections.equip:Disconnect()
	end
	if connections.unequip then
		connections.unequip:Disconnect()
	end
	
	makeVisible()
	
	swordDummy.Transparency = 0
	for i, v in swordDummy:GetChildren(_, true) do
		if v:IsA("BasePart") then
			v.Transparency = 0
		end
	end
	
	Anims[1]:Play()
	connections.unequip = Anims[1]:GetMarkerReachedSignal("AttachWeapon"):Connect(function()
		--print("receive")
		makeInvisible()
	end)
	
	task.wait(0.5)
	
	swordDummy.Transparency = 1
	for i, v in swordDummy:GetChildren(_, true) do
		if v:IsA("BasePart") then
			v.Transparency = 1
		end
	end
	
	disableTouch()
	
end

makeInvisible()

Tool.Equipped:Connect(OnEquip)
Tool.Unequipped:Connect(OnUnequip)

Tool.Activated:Connect(function()	
	if debounce == false then
		debounce = true
		Anims[2]:Play()
		--[[
--This returned either same nonsense that the new event fire does, or just nil
		Tool.Handle.Hitbox.Touched:Connect(function(hit)
			damageEvent:FireServer(Tool.Name, 10, localPlayer.DisplayName, hit, 2)
		end)]]--
	elseif debounce == true then
		return nil
	end
	
	task.wait(2.5)
	print("ready")
	debounce = false
end)

What I have of the serverscript inside the hitbox of the sword tool:

local damageEvent = game.ReplicatedStorage:WaitForChild("Damage")
local debounce = false
--local waitTime = 0.5

damageEvent.OnServerEvent:Connect(function(player, weapon, damage, hit, weaponDebounce)
	print("yes")
	print(player, weapon, damage, hit, weaponDebounce)
end)

The output should be, in order:

Maytronics13, TestSword, 10, (Whatever rig part), 2

The output I am currently receiving:

Maytronics13, Maytronics13, TestSword, 10, (Whatever rig part)


I’ve never experienced anything like this, and don’t know how to go about fixing it. I’ve already tried working with only a hit value, but it never returns anything beside nil.
The values do print in correct order (technically), but they somehow all have each others values, or just the wrong one (except damage, which is correct)

Does anyone know how I can fix this? Thank you!

Hey there. OnServerEvent has a player parameter that will always be sent. It is just the player that fired the remote event. You can correct your code by changing the hitbox script to:

local damageEvent = game.ReplicatedStorage:WaitForChild("Damage")
local debounce = false
--local waitTime = 0.5

damageEvent.OnServerEvent:Connect(function(player, weapon, damage, attacker, hit, weaponDebounce)
	print("yes")
	print(weapon, damage, attacker, hit, weaponDebounce)
end)

Edit: You can also remove the attacker parameter and instead use the built-in player parameter

1 Like

Ah, thank you! I forgot it had a player parameter.

However, there’s still an issue. In the print statements, the arguments still have the completely wrong values.

--Attacker was removed and replaced by player, which is the LocalPlayer.
damageEvent.OnServerEvent:Connect(function(player, weapon, damage, hit, weaponDebounce)
print(player, weapon, damage, hit, weaponDebounce)

Returns the following:

Maytronics13, Maytronics13, TestSword, 10, (Any part of rig)

My concern is that the arguments are somehow mixing their values together. Weapon, which should be the name of the tool, is the LocalPlayer, and I have no idea how that’s being set.

Thank you again, though!

You are firing the remote event with:

	damageEvent:FireServer(
--The comments here are what these are SUPPOSED to return, in order.
        LocalPlayer,              --LocalPlayer
        Tool.Name,                -- weapon name (TestSword)
		10,                       -- damage amount (10)
		otherPart,                -- the part we actually hit (part of rig [limb, rootpart, etc.])
		2                         -- weapon debounce for the server side (2)
	)

So you would recieve this:

player ->      Maytronics13
LocalPlayer -> Maytronics13
Tool.Name ->   TestSword
10 ->          10
otherPart ->   (Any part of rig)
2 ->           2

Which is exactly what you are receiving. There is simply a disconnect in what you are sending through and what parameters you think you are receiving.

Edit: Correct your code to

	damageEvent:FireServer(
        Tool.Name,                -- weapon name (TestSword)
		10,                       -- damage amount (10)
		otherPart,                -- the part we actually hit (part of rig [limb, rootpart, etc.])
		2                         -- weapon debounce for the server side (2)
	)

Edit 2: I removed LocalPlayer because the player parameter is actually built-in.

2 Likes

Thank you for the clarification! Didn’t realize I didn’t have to write the argument anyway.

With some additional editing, it works fine! Thank you :glowing_star:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.