Localscript to serverside

Hello.

I have a knife grab script. The local script that detects clicks etc, is in startercharacterscripts.

The serverscript is in serverscriptservice.

Problem is that the localscript obviously won’t translate anything to the server side.

Remote events would be an answer, but i’m not sure how to implement them in this scenario.

If you would like to give insight for me to fix the problem, feel free to reply.

1 Like

Could you show us your script(s)?
I don’t really understand what you are trying to do

1 Like

Check out this documentation, it may help you a lot.

1 Like

Grab knife script. User clicks when tool is equipped and does “grab” animation.

if hitbox infront of player is hit and it’s a humanoid and not the User, then victim gets welded onto the hitbox.

if user clicks when victim is welded onto hitbox, the user will do a slit animation, killing the victim and repeating the cycle

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Create the GrabAnimation instance
local grabAnimation = Instance.new("Animation")
grabAnimation.AnimationId = "rbxassetid://77325448756458" -- Use your actual animation ID
grabAnimation.Name = "GrabAnimation" -- Name it for reference

local grabbedAnimation = Instance.new("Animation")
grabbedAnimation.AnimationId = "rbxassetid://99980972320345" -- Use your actual animation ID
grabbedAnimation.Name = "GrabbedAnimation" -- Name it for reference


local slitAnimation = Instance.new("Animation")
slitAnimation.AnimationId = "rbxassetid://82839889243848" -- Use your actual animation ID
slitAnimation.Name = "SlitAnimation" -- Name it for reference

local clickSoundEvent = ReplicatedStorage:WaitForChild("ClickSoundEvent")

local isgrabbing = false
local grabbed = false
local weld
-- Wait for tool to be equipped
player:GetAttributeChangedSignal("ToolEquipped"):Connect(function()
	if player:GetAttribute("ToolEquipped") then
		local humanoid = character:WaitForChild("Humanoid")
		-- Load the animations
		local grabtrack = humanoid:LoadAnimation(grabAnimation)
		local grabbedtrack = humanoid:LoadAnimation(grabbedAnimation)
		local slittrack = humanoid:LoadAnimation(slitAnimation)
		local weldpart = character:FindFirstChild("weldpart")
		-- Input detection
		if grabbed == false then
			local function GetVictim(weldpart, character)
				local Hitbox = workspace:GetPartsInPart(weldpart)

				local HRP = nil

				for _, X in pairs (Hitbox) do
					local Parent = X.Parent
					local IsModel = Parent:IsA("Model")
					local Hum = Parent:FindFirstChildOfClass("Humanoid")
					
					if IsModel and Hum and Parent.Name ~= player.Name then
						print("Found victim:", Parent)
						
						HRP = Hum.RootPart
						
						break
					else
						print("not yourself!")
						
					end
				end

				return HRP
			end
			local hasGrabbed = false -- Tracks if a target is grabbed

			UIS.InputBegan:Connect(function(input)
				if input.UserInputType ~= Enum.UserInputType.MouseButton1 then return end

				local weldpart = player.Character:FindFirstChild("weldpart")
				local victimroot = GetVictim(weldpart)
				if not hasGrabbed then
					-- Attempt to grab
					clickSoundEvent:FireServer()
					grabtrack:Play()
					
					if victimroot then
						-- Perform grabbing actions
						grabbedtrack:Play()
						
						victimroot.CFrame = weldpart.CFrame
						weld = Instance.new("WeldConstraint")
						weld.Parent = weldpart
						weld.Part0 = weldpart
						weld.Part1 = victimroot
						hasGrabbed = true -- Update state to indicate target is grabbed
						print("Target grabbed")
					end
				else
					-- Perform slit action
					slittrack:Play()
					grabbedtrack:Stop()
					task.wait(1)
					local victim = victimroot.Parent
					print(victim, "Killed")
					local health = victim.Humanoid.Health
					health = health - 100
					weld:Destroy()
					hasGrabbed = false -- Reset state to allow grabbing again on next click
					print("Target released with slit")
				end
			end)
		end
	end
end)

This is the local script. Everything here should happen on the server, but since it involves clicking, I wanted it to be in a local script.

One problem are the variables. How do I get these variables inside the remote event function? I have a welding function which is connected to a remote event.

How do I get the weld- and weldpart variable inside the remote event function?

If they’re Instances, you can send them through the network by sending their reference, such as RemoteEvent:FireServer(weld, weldpart).

1 Like

Oh yeah, I almost forgot that I could do this.

So I got a problem now. I can’t access the victim humanoidrootpart’s CFrame.

it displays attempt to index nil with CFrame

Code:

if victimroot and victimroot:IsA("BasePart") then
		print(victimroot)

		victimroot.CFrame = weldpart.CFrame
	else
		print("Error: victimroot is invalid")
	end

How is it possible that it gets through the if statement, but displays a nil error?

It’s not good practice to use the assistant. This is probably why, you don’t properly understand coding.

1 Like

Those lines create three animations.

I did use AI to see where a problem was earlier, it leaves a lot of notes which I didn’t have before.

This is my first time needing to use remote events, so you can understand why I need help from others :rofl:

Using AI to blatantly write code is obviously bad. I use AI for insight and checking, where an error could occur.

Simply, code is written by me, but if AI has a solution to a problem I don’t know how to solve, then AI is obviously useful.

1 Like

Check if weldpart exists with print(weldpart).

Me and my brother figured it out.

We forgot to add the player parameter to defining the remote function.

So it mistaked the victimroot for the user. :rofl:

I got it fixed and it works now.

1 Like

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