So i’ll keep it straight, I have set a local variable for a script and when I receive a value for it I set it as that value. But when I check out that value it returns as nil.
local obj -- The variable
game.ReplicatedStorage.Events.TriggerEvent.OnClientEvent:Connect(function(objNew)
obj = objNew
print(obj) -- Prints Linkmon99
end)
script.Parent.MouseButton1Click:Connect(function()
local hrp = game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")
if hrp and obj then -- obj seems to be nil here
game.ReplicatedStorage.Events.HumanoidDescription:FireServer(obj)
else
print(hrp)
print(obj)
end
end)
while wait() do -- Just for testing to see if obj is nil or not
print(obj) -- On first cycle prints nil, next cycle prints Linkmon99 and repeats
end
The obj value I am expecting here is the player character of Linkmon99 which is present in workspace. But with the while true loop I get the variable to be equal to nil the first time and the character the second.
You show the output, but it only shows prints for line 18 and none of the other prints.
Try this with more descriptive print statements and it’ll show which event is firing first:
local obj -- The variable
game.ReplicatedStorage.Events.TriggerEvent.OnClientEvent:Connect(function(objNew)
obj = objNew
print("TriggerEvent: ", obj)
end)
script.Parent.MouseButton1Click:Connect(function()
local hrp = game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")
if hrp and obj then -- obj seems to be nil here
game.ReplicatedStorage.Events.HumanoidDescription:FireServer(obj)
else
print("Click detected. hrp = " , hrp, " obj = ", obj , " objNew = ", objNew)
end
end)