Script wont work outside of tool

I’m trying to make a script that assigns a value to a person which will be their role but outside of the tool it doesn’t work and when in the tool it may assign the same role multiple times

local players = game:GetService(“Players”)

local player = players.LocalPlayer

local Math = math.random(1,6)

local Role = player.Character:FindFirstChild(“Role”)

local card = player.Character.Card or game.Workspace.Card

print(Math)

if Math == 1 then

Role.Value = “Mafia”

card.Frame.Mafia.Visible = true

elseif Math == 2 then

Role.Value = “Doctor”

card.Frame.Doctor.Visible = true

elseif Math == 3 then

Role.Value = “Sheriff”

card.Frame.Sheriff.Visible = true

elseif Math == 4 then

Role.Value = “Tanner”

card.Frame.Tanner.Visible = true

elseif Math == 5 then

card.Frame.TownsPerson.Visible = true

Role.Value = “TownsPerson”

elseif Math == 6 then

card.Frame.TownsPerson.Visible = true

Role.Value = “TownsPerson”

print(Role.Value)

end

any help would be appreciated

Is the script a local script? Local scripts can only work in items, player scripts, and in replicated storage.

Two problems:

“Script wont work outside of tool”

  1. this is a localscript, so it won’t be seen by other players. You need to create a serverscript to add roles, so I suggest you to fire an event from client only and do all role-giving thing on the server.

“it may assign the same role multiple times”

  1. you should check if the given role has been given / the given player has a role already. You need to clean-up roles/players accordignly (I don’t know what it your aim, this is why I can’t add an exact “how to” way)

It is an local script but I also tried a normal script and both haven’t worked

Did you copy the code from the Local script directly onto the script?

yes and it said, attempt to index nil with ‘Character’

Use CharacterAdded:Wait()
so local Role = player.Character:FindFirstChild(“Role”) or player.CharacterAdded:Wait().Role

I tried it but it didn’t work. I got the same error

I fixed the tool part now I need the part that assigns multiple of the same role not to