"Unable to cast value to object"

Number values (.Changed) dont give the player. So its not working.

1 Like

The Parent.Changed event will not give you a new object or player, but in fact a string. you could see what this is by using print(player) What is this script supposed to do? When you are moving Health around that is causing the error.

i used a mining system so the health get subtracted by 1 and if the health is 0 the player gets the currency called “Ores”

so. how would i fix this?
(Char limit)

I will need to know the following information and likely more:

Why is the Health script parent.Changed connected?
What is connected to the RockDestroyed remote event? Why does it need player from Health?

My only guess would be to make a playeraddedevent and put the .Changed event in there and pass the player variable in the playaddedevent. I am not sure if it will cause problems, else you have to change the logic of ur code.

game.Players.PlayerAdded:Connect(function(player)
	script.Parent.Changed:Connect(function()
		print("current health: ".. health.Value)
		text.Text = script.Parent.Value.."/"..maxhealth
		if health.Value == 0 or health.Value < 1 then
			script.Parent.Parent:Destroy()
			print("destroy rock")
			game:GetService("ReplicatedStorage").RockDestroyed:FireClient(player)
		end
	end)
end)

the health script is subtracted by 1 from the mining remote event which is fired from this script :

local pickaxe = script.Parent
local canmine = pickaxe.CanMine
local anim = pickaxe.Animation
local players = game:GetService("Players")
local player = players:GetPlayerFromCharacter(pickaxe.Parent) -- get player if tool is equipped
local character
local animtrack
local humanoid
local ismining = false
local isactive = false
local animplaying = false
local pickaxehitbox = pickaxe.Pickaxepart.PickaxeHitbox
local mining = game:GetService("ReplicatedStorage").Mining
local sc = game:GetService("ReplicatedStorage").ServerConnector
local pickvalue = game:GetService("ReplicatedStorage").Tool.CurrentPickaxe
local playermod = require(game:GetService("Players").LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"))
local controls = playermod:GetControls()
local dc2 = game:GetService("ReplicatedStorage").dc2
local cs = game:GetService("ReplicatedStorage").ChangeStrength


if pickaxe.Parent:IsA("Backpack") then -- checks if tool is in backpack
	player = pickaxe.Parent.Parent -- since backpack is always a child of a player, we get player from the backpack's parent
end


local function CharAdded(char)
	character = char
	humanoid = character:WaitForChild("Humanoid")
	animtrack = humanoid:LoadAnimation(anim)
end

if player.Character then CharAdded(player.Character) end
player.CharacterAdded:Connect(CharAdded)

canmine.Value = true	

pickaxe.Activated:Connect(function()
	if canmine.Value == true and animplaying == false then
		dc2:FireServer()
		print("activated")
		isactive = true
		animtrack:Play()
		canmine.Value = false
		animplaying = true
		wait(3)
		animplaying = false
		pickaxehitbox.Touched:Connect(function(hit)
			if ismining == false then
				if hit.Name == "Hitbox" or hit.Parent == "Rock" then
					print("mining")
					sc:FireServer()
					mining:FireServer()
					ismining = true
					local particle = hit:WaitForChild("ParticleEmitter")
					particle.Enabled = true
					if ismining == true then
						wait(1)
						ismining = false
						particle.Enabled = false
					end
				end
			end
		end)
		task.wait(1)
		canmine.Value = true
	end
end)

if isactive == true then
	wait(1)
	isactive = false
end
the rock destroyed remote event is connected to another local script which is this : 
local repstorage = game:GetService("ReplicatedStorage")
local dc = repstorage.DisableControl
local LocalPlayer = game:GetService("Players").LocalPlayer
local Controls = require(LocalPlayer.PlayerScripts:WaitForChild("PlayerModule")):GetControls()

dc.OnClientEvent:Connect(function()
	print("control disabled")
	Controls:Disable()
	task.wait(2.5)
	Controls:Enable()
	print("control enabled")
end)

game:GetService("ReplicatedStorage").RockDestroyed.OnClientEvent:Connect(function()
	game:GetService("ReplicatedStorage").RockDestroyed2:FireServer()
end)

it returns an error if i don’t put the player instance in it for some reason

thank you! i’ve been pretty confused with this script for a while but i didn’t know it was just a small erorr, still tysm!

Yea no problem. But like i said, I am not sure if it 100% works. You can test it on a local server with 2 people

2 Likes

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