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 std::string" when looking for player?

Made a ModuleScript for presenting a player with a choice. My game worked fine until it got to running the following code:

local c = {
	'workspace',
	'terrian',
	'terrain',
	'spawnlocatoin',
	'lighting',
	'Great',
	'ServerScriptService'
}
Module:PromptChoice('KicksForTricks56','Services',c,'ServiceChoice')

But, currently, the script gives:

13:09:04.619 - Unable to cast value to std::string
13:09:04.630 - Stack Begin
13:09:04.664 - Script 'ServerScriptService.ChoiceScript', Line 12 - method PromptChoice
13:09:04.665 - Script 'ServerScriptService.Script', Line 12
13:09:04.667 - Stack End

What's wrong with the module script? For reference, here it is:

local http = game:GetService("HttpService")
local instructionMessages = {
	["mobile"] = "press an option with your finger",
	["gamepad"] = "press View, then nagivate to an option with the Left Stick and press A",
	["computer"] = "click an option with your mouse/touchpad"
}
local wrapper = "(To make your choice, %s. Choose wisely!)"
local module = {}

function module.PromptChoice(player, caption, choices, identifier)
	local id = ""
	local playerA = game:GetService("Players"):WaitForChild(player)
	if playerA == nil or playerA.Parent == nil then
		error("Player does not exist")
		return false
	end
	if identifier == nil or identifier == '' then
		id = identifier
	else
		id = math.random(1,2000)
	end
	local json = http:JSONEncode(choices)
	local gui = script.ChoiceGui:Clone()
	local event = Instance.new("RemoteEvent")
	event.Name = player.."Choice"..id
	event.Parent = game.ReplicatedStorage
	gui.EventObject.Value = event
	gui.JSONChoices.Value = json
	gui.Caption.Text = caption
	gui.Instructions.Text = string.format(wrapper,instructionMessages[player.input.Value])
	
	gui.Parent = playerA.PlayerGui
	
	event.OnServerEvent:Connect(function(choice)
		local GoodToDo = false
		for i=1,#choices do
			if choices[i] == choice then
				GoodToDo = true
			end
		end
		if not GoodToDo then
			playerA:Kick("Don't know why you'd want to exploit on a game like this? Lol")
		end
		return choice
	end)
end

return module

1 answer

✓ Accepted answer

The issue is the way you're calling your module's function. You need to fire the function with . not :

Module.PromptChoice('KicksForTricks56', 'Services', c, 'ServiceChoice')

When you use the : instead, it autodeclares a self value. Therefore:

Module:PromptChoice('KicksForTricks56', 'Services', c, 'ServiceChoice')
--Is the same as
Module.PromptChoice(Module, 'KicksForTricks56', 'Services', c, 'ServiceChoice')

This is causing a problem because when you try to use the player argument you passed, thinking it's equal to KicksForTricks56, it's really equal to your module.

1 comment

0

Thanks for your answer!

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