Need Help with Tool Ability Doing Nothing - Unable to cast value to object

Haven’t posted here in a while but here goes nothing.
For staters I’m relatively new with Lua and coding in Roblox in general as I only have experience with VB and a little bit of python so bear with me.

I am trying to create an ability in the form of a tool where it places a premade cube anywhere where the player clicks which would act as a barrier supposedly to trap players inside of it. Here is an image of the cube I made for reference.

After the cube is placed a cooldown is to be displayed and after that the cube is to be destroyed and the player is able to place another.

Now comes the problem. Nothing happens at all when I activate the tool by clicking. Not sure what I am doing wrong. I watched numerous tutorials to figure out what I am doing wrong. I’m also relatively new to using remote events and functions so maybe that might be it. Either way nothing is happening and now error appears in the output window so I have no idea where to go from here. I’ve checked over and over and to my limited knowledge everything seems like it should work but clearly not since nothing is happening.

A few more vital information id like to add:

The Explorer tab - Here you can see where I placed everything:
image

Local Script:

local tool = script.Parent
local toolname = tool.name
local rs = game:GetService("ReplicatedStorage")
local CubeEvent = rs:WaitForChild("Merlin Attacks"):WaitForChild("Events"):WaitForChild("Cube")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local CooldownEvent = rs:WaitForChild("Merlin Attacks"):WaitForChild("Events"):WaitForChild("Cooldowns")


tool.Activated:Connect(function()
	local MousePos = Mouse.Hit.Position
	if MousePos == nil then return end
	CubeEvent:FireServer(MousePos)
end)

local function CooldownTimer(Cooldown)
	for i = Cooldown, 0, -1 do
		wait(1)
		tool.Name = toolname .. " (Cooldown: " .. i .. ")"
	end
	tool.Name = toolname
end

CooldownEvent.OnClientEvent:Connect(CooldownTimer)

Server Script:

local Debounce = {}
local Cooldown = 20
local rs = game:GetService("ReplicatedStorage")
local CubeEvent = rs:WaitForChild("Merlin Attacks"):WaitForChild("Events"):WaitForChild("Cube")
local PerfectCube = rs["Merlin Attacks"]["Perfect Cube"]
local CooldownEvent = rs:WaitForChild("Merlin Attacks"):WaitForChild("Events"):WaitForChild("Cooldowns")
local function CreateCube(player,MousePos)
	if table.find(Debounce,player.name) ~= nil then
        print(player.name " is on cooldown!")
	else
	    print(player.Name .. " created a Perfect Cube.")
	    local CubeClone = PerfectCube:Clone()
	    CubeClone.Parent = game.Workspace
		CubeClone:SetPrimaryPartCFrame(CFrame.new(MousePos))
		table.insert(Debounce,player.name)
		CooldownEvent:FireClient(Cooldown)
		task.wait(Cooldown)
		CubeClone:Destroy()
		Debounce[table.find(Debounce,player.name)] = nil
	end 
end

CubeEvent.OnServerEvent:Connect(CreateCube)

Any help would be appreciated as I continue to try and find the issue.

Edits: I fixed some issues and now my current issue is this error when passing values.

If anyone knows what this error means that would be helpful.

Newest edit: I was able to get the cube to place though its always in the ground now so I’m not sure how to fix that and there is a new error with the server script passing the cooldown value through the
remote function on the client side.

New error:

thank you for all your help

1 Like

Is your tool require hanlde property on?

2 Likes

what do you mean by that? Is there something I need to toggle? My tool isn’t supposed to have like a a visible object in hand as your supposed to be able to equip it then activate thus there is no handle.

Edit: I just disabled the require handle property thank you for that. There are still more issues though with CFrame

1 Like

If u want mouse click use user input service or context action service, tool.Activated is fired when u do Tool:Activate() so you have to manually call the function yourself.

1 Like

I’ve messed with it and I’d have to disagree because now its firing and the cube is being placed though not perfectly and the cooldown is erroring now. So the function is working properly as it should at least. l will be updating my post to explain my new issues.

1 Like

CFrame has coordinates for X, Y, Z. If you want your cube to be placed up, try using

CubeClone:SetPrimaryPartCFrame(CFrame.new(MousePos) * CFrame.new(0,3,0))

On the local script line 2, you forgot to capitalize the name.

Also instead of using game.Workspace, use workspace.

1 Like

Thank you so much you’ve been a really big help! I messed around with it and go it to place nicely! Sorry if it’s too much to ask but do you know how I can fix the error with
unable to cast value to object
found on server script line 16. It happens when I’m trying to pass the cooldown value when firing the client. The cooldown value is supposed to then work so that I can display a cooldown the increments downwards as the tool name. The part also doesn’t destroy itself which I’m guessing is due to the code not even running due to the error.

CooldownEvent:FireClient(Cooldown) – FireClient needs the first parameter to have a player

use FireAllClients if you’re not firing to an individual player.

FireClient(player, Cooldown) should be fine.

Ah ok thank you! My code FINALLY works perfectly!

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