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

How to convert object to string or find a different way around my problem?

On lines 13/14 of the Event Receive Script I get the error "ServerScriptService.RecieveEvents:13: bad argument #2 to '?' (string expected, got Object)" How can i fix this or find a way around it?

--Event FireScript

-----------------------Events---------------------------------------
local CharacterChosen = game:GetService("ReplicatedStorage").CharacterChosen
----------------------Workspace Models---------------------------------------
local RecruitLoadoutModel = workspace.Recruit
local DecoyLoadoutModel = workspace.Decoy
local StrikerLoadoutModel = workspace.Striker
local CaliaLoadoutModel = workspace.Calia
local BlasterLoadoutModel = workspace.Blaster
-----------------------Player Models---------------------------------------
local RecruitCharacterModel = game:GetService("ServerStorage").Recruit
local StrikerCharacterModel = game:GetService("ServerStorage").Striker
local DecoyCharacterModel = game:GetService("ServerStorage").Decoy
local CaliaCharacterModel = game:GetService("ServerStorage").Calia
local BlasterCharacterModel = game:GetService("ServerStorage").Blaster
local Player = game.Players.LocalPlayer
-----------------------Functions---------------------------------------
function Recruit()
    print("Clicked")
	local Character = Recruit
    if RecruitLoadoutModel.Availability.Value == true then
		print("trying to fire event")
        CharacterChosen:FireServer(Player, Character)
		print("fired from local")
	end
end
function Striker()
    print("Clicked")
	local Character = Striker
    if StrikerLoadoutModel.Availability.Value == true then
        CharacterChosen:FireServer(Player, Character)
    end
end
function Decoy()
    print("Clicked")
	local Character = Decoy
    if DecoyLoadoutModel.Availability.Value == true then
        CharacterChosen:FireServer(Player, Character)
    end
end
function Calia()
    print("Clicked")
	local Character = Calia
    if CaliaLoadoutModel.Availability.Value == true then
        CharacterChosen:FireServer(Player, Character)
    end
end
function Blaster()
    print("Clicked")
	local Character = Blaster
    if BlasterLoadoutModel.Availability.Value == true then
        CharacterChosen:FireServer(Player, Character)
    end
end
-----------------------Connect Functions---------------------------------------
script.Parent.Recruit.MouseButton1Click:connect(Recruit)
script.Parent.Striker.MouseButton1Click:connect(Striker)
script.Parent.Decoy.MouseButton1Click:connect(Decoy)
script.Parent.Calia.MouseButton1Click:connect(Calia)
script.Parent.Blaster.MouseButton1Click:connect(Blaster)

--Event ReciveScript

--Receiving RemoteFuction Script
-----------------------Events---------------------------------------
local CharacterChosen = game:GetService("ReplicatedStorage").CharacterChosen
-----------------------Player Models---------------------------------------
local TheRecruitCharacterModel = game:GetService("ServerStorage").Recruit
local TheStrikerCharacterModel = game:GetService("ServerStorage").Striker
local TheDecoyCharacterModel = game:GetService("ServerStorage").Decoy
local TheCaliaCharacterModel = game:GetService("ServerStorage").Calia
local TheBlasterCharacterModel = game:GetService("ServerStorage").Blaster
-----------------------Functions---------------------------------------
function ChoseCharacter(Player, Character)
	print("fired from server")
	workspace[Character].Availability.Value = false
	workspace[Character].Head.GUI.Availability.Text = Player.Name
	local ClonedModel = game:GetService("ServerStorage")[Character]:Clone()
	ClonedModel.Parent = workspace
	ClonedModel.Name = Player.Name
    Player.Character = ClonedModel
    workspace.CurrentCamera:Destroy()
    wait()
    workspace.CurrentCamera.CameraType = "Custom"
    workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character[("Humanoid")]
end
-----------------------Connect Functions---------------------------------------
CharacterChosen.OnServerEvent:Connect(ChoseCharacter)

2 comments

0

Why do you have "[Character]" on lines 13 and 14?

0

Because the character name is chosen depending on which button they pressed, so the name is never the same. So i used local Character as the model name.

1 answer

✓ Accepted answer

The script is erroring because you are trying to get the Character by searching it on the workspace using the actual object, not it's name.

An example of this would be:

local Person = workspace.Person
local Character1 = workspace[Person.Name] -- This will work! The argument provided is a string.
local Character2 = workspace[Person] -- This will not work! The argument is an object.

What you are doing is redundant, too. You don't need to index it like "workspace[Character.Name]" since you already got the Character object. Using just "Character" will do fine.

Also, unless the second argument "Character" is a character of a different player, you should just have the first argument "Player" and use the Character property of the Player object.

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