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

Error "Unable to cast Instance to int"?

I am trying to make a script that lets you get a award by using a chat command, and i'm using a RemoteEvent to do it. The problem is I get the " Unable to cast Instance to int" on line 8 of the second script.

LOCAL SCRIPT:

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://636749560"
local animtrack = nil
local canplay = true
local playe = game.Players.LocalPlayer
local char = playe.Character
local badgeid = 637991953
local id = playe.userId

local Lighting = game.Lighting
local Event = Lighting:WaitForChild("RemoteEvent")

function processCommand(message,recipient)
    if message == "/e dab" then
        print("part1")
        if canplay then
            canplay = false
            animtrack = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animation)
            animtrack:Play()
        -- Call to the server
        -- We're going to give it some arguments that the AwardBadge function needs 
        -- So it needs the UserID and BadgeID
        	Event:FireServer(playe.UserId, badgeid) -- Fires the arguments to the server
			print("event fired")
            canplay = true
        end
    end
end

playe.Chatted:connect(processCommand)

SERVER SCRIPT:

local badgeService = game:GetService("BadgeService")
-- Set up the RemoteEvent (It will be in Lighting)
local Lighting = game.Lighting
local Event = Lighting:WaitForChild("RemoteEvent")

function awardBadge(id, badgeid) -- The function in which awards the player
	print("awarding")
    badgeService:AwardBadge(id, badgeid) -- Awards the player - BOOM! :D
	print("awarded")
end

Event.OnServerEvent:connect(awardBadge) -- This listens to when the :FireServer() is called

Also, the tips are there because it was from someone who helped me earlier get this far. Thanks!

6 comments

0

BTW Remote Event is already in lighting

0

On line 23 of the localscript, change 'playe.UserId' to 'id' and see what happens

0

@qMeta It still doesn't work.

0

Possibly put the animation somewhere... so add a line after you give the animation its id and put 'animation.Parent = game.Players.LocalPlayer'

1

Change the function to "badgeService:AwardBadge(-NEWLINE- id, -NEWLINE- badgeid)" to see wich part of the line errors.

0

Line 23 should be userId

1 answer

✓ Accepted answer

Your coding is a bit strange, you're not using your variables as much as you should. I.e, you're defining things and not using them.

local animtrack
local canplay = true
local player = game.Players.LocalPlayer
player.CharacterAdded:connect(function(char)
	local animation = Instance.new("Animation")
	animation.AnimationId = "rbxassetid://636749560"
	animation.Parent = char
	animtrack = player.Character.Humanoid:LoadAnimation(animation) --Fixed
	local badgeid = 637991953
	local id = player.userId
	local Lighting = game.Lighting
	local Event = Lighting:WaitForChild("RemoteEvent")
	player.Chatted:connect(function(message,recipient)
	    if string.lower(message) == "/e dab" then --this will have a better change of finding it
	        print("part1")
	        if canplay then
	            canplay = false
	            animtrack:Play()
	            Event:FireServer(id, badgeid) -- Fixed
	            print("event fired")
	            canplay = true
	        end
	    end
	end)
end)
--This should work, but, assuming you have fe on, it will only show the animation for the player. To fix this, simply make and run the animation on the server script. =)

2 comments

0

Thanks

0

I still get the same error though.

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