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

Why is this only working in studio and not in play mode?

So I tested this out by pressing F6 and going into test mode, and it works in that, however it doesn't work when I play it in an actual server. Any idea why?

Here is my code:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local character = player.Character

if not character or character.Parent == nil then
	repeat wait() until character ~= nil
end

local selection = Instance.new("SelectionBox",player.PlayerGui)
selection.Color = BrickColor.new("Medium stone grey")

local character = player.Character

local valid_target = character.valid_target

mouse.Icon = "http://www.roblox.com/asset/?id=184642130"

mouse.Move:connect(function()
	local target = mouse.Target
	if target ~= nil then
		local hum = target.Parent:FindFirstChild("Humanoid")
		local enemy = target.Parent:FindFirstChild("enemy")
		local player = target.Parent:FindFirstChild("player")
		local book = target.Parent:FindFirstChild("book")
		local friend = target.Parent:FindFirstChild("friend")
		if hum and target.Parent:IsA("Model") and not player and enemy then
			mouse.Icon = "http://www.roblox.com/asset/?id=165194824"
			selection.Adornee = target.Parent
			valid_target.Value = true
		elseif hum and target.Parent:IsA("Model") and not player and book then
			mouse.Icon = "http://www.roblox.com/asset/?id=186747208"
			selection.Adornee = target.Parent
			valid_target.Value = true
		elseif hum and target.Parent:IsA("Model") and not player and friend then
			mouse.Icon = "http://www.roblox.com/asset/?id=186749391"
			selection.Adornee = target.Parent
			valid_target = true
		elseif hum and target.Parnet:IsA("Model") and player then
			mouse.Icon = "http://www.roblox.com/asset/?id=186750107"
			selection.Adornee = target.Parent
			valid_target = true
		else
			mouse.Icon = "http://www.roblox.com/asset/?id=184642130"
			selection.Adornee = nil
		end
	else
		mouse.Icon = "http://www.roblox.com/asset/?id=184642130"
		selection.Adornee = nil
	end
end)

2 comments

0

Is this in a LocalScript?

0

Yes.

1 answer

✓ Accepted answer

You have a loop,

repeat
	wait()
until character ~= nil

However, nothing here is changing character, so if it fails once, it will fail forever.

You need to re-check the player.Character each time:

-- First Options
repeat
	wait()
	character = player.Character
until character

-- Second Option
repeat
	wait()
until player.Character
local character = player.Character

The if around it doesn't make a big difference (stops one wait()) so I would probably just remove it.


I like the second option better since it means you don't need to define local character before the loop, when it won't necessarily even have a value.

1 comment

0

Thanks a lot! I was having trouble because of this in all of my scripts, this helped a ton.

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