Help with server-client relation please

I have a tool that has local and server scripts inside and Remote event.
image

when you activate the tool (click while equipped) local script sends a remote event to server with desired information. Server script in tool receives it and It copies the part from replicated storage several times and parents it to works space.


SO the part that is in replicated storage has script inside that is also connected to that remote event. And when i click the tool i want that script to accept that remote and get info. But since it doesnt and i have no clue why i cant make it so that spikes wont damage you. (they should dmg everything besides owner of tool).

image

local script in tool:

local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Event = tool.RemoteEvent
local char = player.Character

local ts = game:GetService("TweenService")
local ti = TweenInfo.new(.01)


local function onActivated()
	local MouseHit = mouse.Hit.Position
	local MouseHitPosition = Vector3.new(MouseHit.X, char.HumanoidRootPart.Position.Y, MouseHit.Z)
	
	
	local goal = {}
	goal.CFrame = CFrame.new(char.HumanoidRootPart.Position, MouseHitPosition)
	local tween = ts:Create(char.HumanoidRootPart, ti, goal)
	tween:Play()
	
	--char.HumanoidRootPart.CFrame = CFrame.new(char.HumanoidRootPart.Position, MouseHitPosition)
	--Event:FireServer(mouse.Hit.Position)
	Event:FireServer(CFrame.new(char.HumanoidRootPart.Position, MouseHitPosition))
	
	
end

local function onUnequipped()
	--print("tool was unequipped")
end

local function onEquipped()
	--print("tool was equipped")
end

tool.Activated:Connect(onActivated)
tool.Equipped:Connect(onEquipped)
tool.Unequipped:Connect(onUnequipped)

server script in tool

local tool = script.Parent
local Players = game:GetService("Players")
local Event = tool.RemoteEvent
local ts = game:GetService("TweenService")



Event.OnServerEvent:Connect(function(player, cframe)
	print("1st script accepted event")
	local char = player.Character
	local spikePos = nil
	local Height = 0
	for i = 0, 0.7, 0.1 do
		if i == 0 then
			local part = game.ReplicatedStorage.Ground:Clone()
			part.Anchored = true
			part.CFrame = cframe * CFrame.new(0, -1.5, -10)
			spikePos = part.CFrame
			part.CFrame = cframe * CFrame.new(0, -5, -10)
			Height = part.Size.Y
			part.Parent = game.Workspace
			local goal = {}
			goal.CFrame = spikePos
			local ti = TweenInfo.new(1-i)
			local tween = ts:Create(part, ti, goal)
			tween:Play()
			task.wait(i)
			--tween.Completed:Wait()
		else
			Height += 2
			local part = game.ReplicatedStorage.Ground:Clone()
			part.Size = Vector3.new(part.Size.X, Height, part.Size.Z)
			part.Anchored = true
			part.CFrame = spikePos * CFrame.new(0, 1, -5)
			spikePos = part.CFrame --* CFrame.new(0, 1, 0)
			part.CFrame = spikePos * CFrame.new(0, -1.5-Height, 0)
			part.Parent = game.Workspace
			local goal = {}
			goal.CFrame = spikePos
			local ti = TweenInfo.new(1-i)
			local tween = ts:Create(part, ti, goal)
			tween:Play()
			task.wait(i)
			--tween.Completed:Wait()
			
		end
		
	end
	
			--[[local part = game.ReplicatedStorage.Ground:Clone()
			part.Anchored = true
			part.CFrame = cframe * CFrame.new(0, 0, -20)
			part.Parent = game.Workspace]]
end)

server script in part that is in replicated storage:

local part = script.Parent
local flag = true
local char = nil
local player = nil
local humanoid = nil

local tool = game.Workspace.SpikesTweenOwner
local remote = tool.RemoteEvent
local owner = nil



remote.OnServerEvent:Connect(function(plr)
	print("second accepted")
	owner = plr
	print(owner)
end)

part.Touched:Connect(function(hit)
	char = hit.Parent
	humanoid = char:FindFirstChild("Humanoid")
	player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player == owner then
		return
	else
		if flag == true and humanoid then
			flag = false
			humanoid.Health = humanoid.Health - 33
		end
	end
end)

I would be extremely grateful to a soul that could explain me what is the problem! Any advice is appreciated

Can you share your output? What gets printed and what parts aren’t working?

To help you a little bit, if the prints are printing but nothing is happening, then it’s likely that your if i == 0 then statement or if player == owner then statement are the problem. You should add prints to inside of those checks and see what’s resulting.

Personally, I’d suggest putting your RemoteEvents into ReplicatedStorage. You’d have to update the Event variable in all of your scripts, but the only one that needs a little more updating would be your SpikeDMG script.

Hopefully this will work for you:

local remote = game:GetService("ReplicatedStorage").RemoteEvent
local owner = nil

remote.OnServerEvent:Connect(function(plr)
	local tool = plr.Character.SpikesTweenOwner

	print("second accepted")
	owner = plr
	print(owner)
end)
1 Like

Try deleting this line or making it a variable in your remote event since the tool isn’t parented to the workspace.

1 Like

Thank you so much, I cant believe i was so focused on the server-client relation that I didn’t even notice that tool was not in workspace as i had it in inventory upon picking it up! Thank you so much :slight_smile:

That really worked out too! My tool variable was a little wrong. Thank you so much for pointing that out!