OnServerEvent not working

Local Script

local Equip = script.Parent
local InvHandler = game.Players.LocalPlayer.PlayerGui.InventoryHandler 
local UpperInv = InvHandler.UpperInv
local MaxPetLimit = UpperInv.MaxPetLimit
local PetLimitText = MaxPetLimit.PetLimitText
local PetPreview = UpperInv.PetPreview
local PetPreviewFrame = PetPreview.PetPreviewFrame
local Im = PetPreviewFrame.PetPreviewImage
-- We just defined all the UI elements 



local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BasicEggPetFolder = ReplicatedStorage.BasicEggPetFolder
local Cat = BasicEggPetFolder.Cat
local Dog = BasicEggPetFolder.Dog
local Mouse = BasicEggPetFolder.Mouse
local Dragon = BasicEggPetFolder.Dragon
local GrassyCat = BasicEggPetFolder.GrassyCat
local TeaMonster = BasicEggPetFolder.TeaMonster
-- Pet Vars

local plr = game.Players.LocalPlayer
local char = plr.Character

local debounce = false

Equip.MouseButton1Click:Connect(function()
	if debounce then return end
	debounce = true
		if Im.Image =="rbxassetid://7620353586" then -- check if the pet you click is a cat 
		local Clo1 = Cat:Clone() -- Clone the cat model
		local StatsValue = Instance.new("StringValue") -- adding a string value inside the model cat to see if it s equipped or not
		StatsValue.Parent = Clo1
		StatsValue.Value = "Equipped"
		Clo1.Parent = char
		
		local EquipPet = ReplicatedStorage:WaitForChild("EquipPet")
		EquipPet:FireServer(Clo1)-- Equip pet is the Remote event
		
	
		end
	debounce = false
		
	end
	end)
	
	
	
	


ServerScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EquipPet = ReplicatedStorage:WaitForChild("EquipPet")
local char = game.Workspace:WaitForChild("Character")
local humRoot = char:FindFirstChild("HumanoidRootPart")

EquipPet.OnServerEvent:Connect(function(player, Clo1)
	Clo1:SetPrimaryPartCFrame(humRoot.CFrame) -- I am trying to position the pet near the player
	print("Worked")
	
end)

I am trying to position the model cat near the player whenever the player clicks the EquipButton on the InventoryGui. So there is no errors it just the I don’t think the server event is working because the Worked print will not print and the pet will not positon and When I click the Equip Button I want the cat to only Clone once but it clones serval times

I am definitely sure it is the variable you are trying to pass through the Remote.

In this attempt, I am sending the name of the “thing”.

local Equip = script.Parent
local InvHandler = game.Players.LocalPlayer.PlayerGui.InventoryHandler 
local UpperInv = InvHandler.UpperInv
local MaxPetLimit = UpperInv.MaxPetLimit
local PetLimitText = MaxPetLimit.PetLimitText
local PetPreview = UpperInv.PetPreview
local PetPreviewFrame = PetPreview.PetPreviewFrame
local Im = PetPreviewFrame.PetPreviewImage
-- We just defined all the UI elements 



local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BasicEggPetFolder = ReplicatedStorage.BasicEggPetFolder
local Cat = BasicEggPetFolder.Cat
local Dog = BasicEggPetFolder.Dog
local Mouse = BasicEggPetFolder.Mouse
local Dragon = BasicEggPetFolder.Dragon
local GrassyCat = BasicEggPetFolder.GrassyCat
local TeaMonster = BasicEggPetFolder.TeaMonster
-- Pet Vars

local plr = game.Players.LocalPlayer
local char = plr.Character

local debounce = false

Equip.MouseButton1Click:Connect(function()
	if debounce then return end
	debounce = true
		if Im.Image =="rbxassetid://7620353586" then -- check if the pet you click is a cat 
		local Clo1 = Cat:Clone() -- Clone the cat model
		local StatsValue = Instance.new("StringValue") -- adding a string value inside the model cat to see if it s equipped or not
		StatsValue.Parent = Clo1
		StatsValue.Value = "Equipped"
		Clo1.Parent = char
		
		local EquipPet = ReplicatedStorage:WaitForChild("EquipPet")
		EquipPet:FireServer(Clo1.Name)-- Equip pet is the Remote event
		
	
		end
	debounce = false
		
	end
	end)
	
	

Then I am searching for that name of the thing" in the player’s character from the server side. Afterwards, changing the position.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EquipPet = ReplicatedStorage:WaitForChild("EquipPet")

EquipPet.OnServerEvent:Connect(function(player, name)
local char = player:WaitForChild("Character")
local humRoot = char:FindFirstChild("HumanoidRootPart")
-- realized you didnt define the actual player's character
	player.Character:FindFirstChild(name):SetPrimaryPartCFrame(humRoot.CFrame) -- I am trying to position the pet near the player
	print("Worked")
	
end)

Maybe? *Edit: Realized that you didn’t define the actual player’s character properly.

Is there an error in the developer console? If so, what is the error?

1 Like

when using FireServer on localscript, it sends player first and then the other parameters.
So it should be

Local Script

EquipPet:FireServer(player, Clo1)

Let me know if it doesn’t work

FireServer automatically sends the player parameter so you can take out that player parameter

@wakeupjavi Character is not a child of player, it is a property. You can instead do

char = player.Character or player.CharacterAdded:Wait()

Are you trying to get the player’s character? If so, just use the code I sent to @wakeupjavi as the player’s character’s name is not “Character”, but it is the player’s username.

1 Like

Correct, yes it is best to wait for the Character.

There is no error in the output

Try replacing:

local EquipPet = ReplicatedStorage:WaitForChild("EquipPet")

for:

local EquipPet = ReplicatedStorage:FindFirstChild("EquipPet")

or add this between the variable and the remote event fire

local EquipPet = ReplicatedStorage:WaitForChild("EquipPet")
while EquipPet == nil do wait() end
EquipPet:FireServer(Clo1.Name)-- Equip pet is the Remote event

or, you could also try setting the variable before the MouseButton1Click function and see if it now fires properly

The “EquipPet” variable might not have been set by the time it tries to fire the remote event, so… i hope this helps

Have you even tried what I suggested? A player’s character is not named ‘Character’, it’s the player’s username. You may want to define char and humRoot inside the OnServerEvent function so it changes for every player who activates it, not one specific character.

--This is the now changed server script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EquipPet = ReplicatedStorage:WaitForChild("EquipPet")
EquipPet.OnServerEvent:Connect(function(player, Clo1)
	Clo1:SetPrimaryPartCFrame(humRoot.CFrame) -- I am trying to position the pet near the player
	print("Worked")
	local char = game.Workspace:WaitForChild("Character")
    local humRoot = char:FindFirstChild("HumanoidRootPart")
end)

The main problem is that the remote event doesnt fire, so…