Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Unable to cast value to object? I'm not sure what this means, my code looks fine.

Hi, I'm making a system where I want to fire a specific client with the value of Experience which is in an object. I know I have the right path to all the things including my variables, I just don't know what is causing this error. Here is my script for server. My local script works perfectly fine, the error is appearing in the server script.

local rS = game:GetService("ReplicatedStorage")
local remoteEvents = rS.RemoteEvents
local dmgEv = remoteEvents.Damaged

dmgEv.OnServerEvent:Connect(function(plr, block, dmg, waitingTime)
	print(block.Name.." is the block")
	if block.Health.Value >= dmg then
		wait(waitingTime)
		block.Health.Value = block.Health.Value - dmg
		if block.Health.Value == 0 then
			plr.Experience.Value = plr.Experience.Value + block.Experience.Value
			block.Anchored = false
			print(block:WaitForChild("Experience").Value) --//Prints
			remoteEvents:WaitForChild("XpNotify"):FireClient(block:WaitForChild("Experience").Value)
		end
	else
		wait(waitingTime)
		if block.Health.Value ~= 0 then
			plr.Experience.Value = plr.Experience.Value + block.Experience.Value
			block.Health.Value = 0
			block.Anchored = false
			remoteEvents.XpNotify:FireClient(block.Experience.Value)
		else
			remoteEvents.XpNotify:FireClient(block.Experience.Value)
			block.Health.Value = 0
			plr.Experience.Value = plr.Experience.Value + block.Experience.Value
		end
	end
end) 

The error occurs on this line

remoteEvents:WaitForChild("XpNotify"):FireClient(block:WaitForChild("Experience").Value)

22:28:39.785 - Unable to cast value to Object

Thanks for your time. All help is appreciated.

1 answer

✓ Accepted answer

You need to pass the player as well in FireClient. This would also apply to all of the FireClients you have.

So:


remoteEvents:WaitForChild("XpNotify"):FireClient(plr,block:WaitForChild("Experience").Value)

Since it needs to know what client to fire to.

The full script with all FireClients fixed:


local rS = game:GetService("ReplicatedStorage")
local remoteEvents = rS.RemoteEvents
local dmgEv = remoteEvents.Damaged

dmgEv.OnServerEvent:Connect(function(plr, block, dmg, waitingTime)
    print(block.Name.." is the block")
    if block.Health.Value >= dmg then
        wait(waitingTime)
        block.Health.Value = block.Health.Value - dmg
        if block.Health.Value == 0 then
            plr.Experience.Value = plr.Experience.Value + block.Experience.Value
            block.Anchored = false
            print(block:WaitForChild("Experience").Value) --//Prints
            remoteEvents:WaitForChild("XpNotify"):FireClient(plr, block:WaitForChild("Experience").Value)
        end
    else
        wait(waitingTime)
        if block.Health.Value ~= 0 then
            plr.Experience.Value = plr.Experience.Value + block.Experience.Value
            block.Health.Value = 0
            block.Anchored = false
            remoteEvents.XpNotify:FireClient(plr, block.Experience.Value)
        else
            remoteEvents.XpNotify:FireClient(plr, block.Experience.Value)
            block.Health.Value = 0
            plr.Experience.Value = plr.Experience.Value + block.Experience.Value
        end
    end
end) 

3 comments

0

Hope this solved your problem.

0

If you use :FireAllClient() you do not have to pass through a player as a parameter. But, this answer is indeed correct.

0

wow my eyes didn't spot that.

This discussion is preserved as it was. New answers, comments, and votes are closed.