Value returned as Nil even though its cloned in the exact same script

Hello, I have a small issue with my script where the NPCDummy is apparently nil even though its just been created by the script:

plr = game.Players.LocalPlayer
RE = script.Parent.InitiateConversation

PlrDummy = game.Workspace.PlrDummy:Clone()
PlrDummy.Parent = plr.PlayerGui.NPCTextGui.PlayerViewport
NPCDummy = game.Workspace.NPCDummy:Clone()
NPCDummy.Parent = plr.PlayerGui.NPCTextGui.NPCViewport


RE.OnClientEvent:Connect(function(NPC, NPCAppearance, AltAppearance)
	NPCDummy.Humanoid:ApplyDescription(NPCAppearance)
	PlrDummy.Humanoid:ApplyDescription(game.Players:GetHumanoidDescriptionFromUserId(plr.UserId))
		-- Initiate Dialouge
end)

image

It’s not due to loading times since the Remote Event is fired after the Player clicks an NPC, so everything would definately be loaded at that point.

I also assumed that the cloned parts are just being deleted but I can still see them on the client. Since this is a local script, I don’t believe that the server needs to see it, not that it needs to.

Any help is appreciated!

3 Likes

oh, this is the script firing the event by the way if it helps:


PS = game:GetService("Players")
PlrtoCopy = PS:GetHumanoidDescriptionFromUserId(36429180)
hum = script.Parent.Humanoid
hum:ApplyDescription(PlrtoCopy)

script.Parent.ClickDetector.MouseClick:Connect(function(Firedplr)
	
	local RE = Firedplr.PlayerGui.NPCTextGui.InitiateConversation
	RE:FireClient(Firedplr, "CoreGiver", PlrtoCopy)
end)

Can you try leaving the argument field for the function in the script firing the event empty, like this:

script.Parent.ClickDetector.MouseClick:Connect(function()
	
	local RE = Firedplr.PlayerGui.NPCTextGui.InitiateConversation
	RE:FireClient("CoreGiver", PlrtoCopy)
end)
1 Like

I could but if I do this then the Remote Event Variable will become nil

Moving the Remote Event to Replicatedstorage to get around the above issue, I now get this error:
image


PS = game:GetService("Players")
PlrtoCopy = PS:GetHumanoidDescriptionFromUserId(36429180)
hum = script.Parent.Humanoid
hum:ApplyDescription(PlrtoCopy)

script.Parent.ClickDetector.MouseClick:Connect(function()
	
	local RE = game.ReplicatedStorage.RemoteEvents.InitiateConversation
	RE:FireClient("CoreGiver", PlrtoCopy)
end)

On the server side you are sending only two arguments whereas on the client side you are trying to get three arguments, and so one of those arguments is shown as nil
And as for npcdummy being nil, can you do a print statement for each action performed like this



local plr = game.Players.LocalPlayer
local RE = script.Parent.InitiateConversation

print("LocalPlayer:", plr.Name)

local PlrDummy = game.Workspace.PlrDummy:Clone()
PlrDummy.Parent = plr.PlayerGui.NPCTextGui.PlayerViewport
print("PlrDummy cloned and parent set to PlayerViewport")

local NPCDummy = game.Workspace.NPCDummy:Clone()
NPCDummy.Parent = plr.PlayerGui.NPCTextGui.NPCViewport
print("NPCDummy cloned and parent set to NPCViewport")

RE.OnClientEvent:Connect(function(NPC, NPCAppearance, AltAppearance)
    print("Received OnClientEvent")
    print("NPC:", NPC.Name)
    print("NPCAppearance:", NPCAppearance)
    print("AltAppearance:", AltAppearance)

    NPCDummy.Humanoid:ApplyDescription(NPCAppearance)
    print("Applied NPCAppearance to NPCDummy")

    local plrDescription = game.Players:GetHumanoidDescriptionFromUserId(plr.UserId)
    print("Player description obtained:", plrDescription)
    PlrDummy.Humanoid:ApplyDescription(plrDescription)
    print("Applied player description to PlrDummy")

    -- Initiate Dialogue
end)



1 Like

I see, I completely forgot that AltAppearance was there since it was part of fixing an older issue I resolved so I forgot to delete it

After adding the print statements, I’ve discovered that the event does fire but NPC and NPCAppearance are printed as nil which is interesting.

NPC is just a string that was set whilst being fired and NPCAppearance is from PlrtoCopy = PS:GetHumanoidDescriptionFromUserId(36429180) which isn’t nothing since its used to apply the description on an NPC and has done so without issue

I’m going to assume that the NPCAppearance didn’t get sent because it’s an instance which apparently cant be sent down to clients, but why is the string nil?

local PS = game:GetService("Players")
local PlrtoCopy = PS:GetHumanoidDescriptionFromUserId(36429180)
local hum = script.Parent.Humanoid
hum:ApplyDescription(PlrtoCopy)

print("Humanoid description applied to NPC.")

script.Parent.ClickDetector.MouseClick:Connect(function()
    print("Mouse clicked on NPC.")

    local RE = game.ReplicatedStorage.RemoteEvents.InitiateConversation
    RE:FireClient("CoreGiver", PlrtoCopy)

    print("InitiateConversation event fired with player description.")
end)

Try this and also print your string

Add player argument in the click detector function too, you cant fire a remote event to a string

1 Like

I already Resolved the issue by the looks of it:

plr = game.Players.LocalPlayer
RE = game.ReplicatedStorage.RemoteEvents.InitiateConversation

PlrDummy = workspace:WaitForChild("PlrDummy"):Clone()
NPCDummy = workspace:WaitForChild("NPCDummy"):Clone()
PlrDummy.Parent = script.Parent.PlayerViewport
NPCDummy.Parent = script.Parent.NPCViewport

--[[PCamera = Instance.new("Camera")
NCamera = Instance.new("Camera")
PCamera.Parent = script.Parent
NCamera.Parent = script.Parent

print(PCamera.Name)
print(NCamera.Name)
print(PlrDummy.PrimaryPart.Name)
print(NPCDummy.PrimaryPart.Name)

PCamera.CFrame = PlrDummy.PrimaryPart.CFrame + CFrame.new(1,0,0)
NCamera.CFrame = NPCDummy.PrimaryPart.CFrame + CFrame.new(1,0,0)

script.Parent.PlayerViewport.CurrentCamera = PCamera
script.Parent.NPCViewport.CurrentCamera = NCamera]]

RE.OnClientEvent:Connect(function(NPC, NPCAppearance)
	print("Received OnClientEvent")
	print("NPC:", NPC)
	print("NPCAppearance:", NPCAppearance)

	NPCDummy.Humanoid:ApplyDescription(game.Players:GetHumanoidDescriptionFromUserId(NPCAppearance))
	print("Humanoid desc Applied")
	PlrDummy.Humanoid:ApplyDescription(game.Players:GetHumanoidDescriptionFromUserId(plr.UserId))
		-- Initiate Dialouge
end)

I added back the player argument, I don’t know why the previous user wanted to see it removed so i just added it back

This caused the String to return as Nil since you can’t do String.Name.

I was correct in my assumption, changing the Description to print the ID instead of the instance itself no longer had it return to nil

(ignore the extra comments in my code I was actually about to make another post for an issue i’m having with viewports and models not showing up in them.)

Thank you both for your help though

1 Like

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