Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get character from script? (Tool)

Asked by
BiIinear 104
5 years ago

I've been struggling on this for half an hour, I'm not sure how to get the character from a normal script. The script is located inside a tool.

01script.Parent.Equip.Player.Value = game.Players:FindFirstChild(script.Parent.Parent.Parent.Name)
02 
03if not game.ReplicatedStorage:FindFirstChild("spellcast_event") then
04    script.Spellcast_event.Parent = game.ReplicatedStorage
05    script.Parent.Output.Disabled = false
06else
07    script.Spellcast_event:Destroy()
08    script.Parent.Output.Disabled = false
09end
10wait(1)
11script:Destroy()

And this is the script where I'm getting the error from.

When I equip: 23:08:54.482 - Players.BiIinear.Backpack.Spellcast.Equip:7: attempt to index global 'player' (a nil value) 23:08:54.483 - Stack Begin 23:08:54.483 - Script 'Players.BiIinear.Backpack.Spellcast.Equip', Line 7 23:08:54.483 - Stack End

When I unequip: 23:08:55.581 - Players.BiIinear.Backpack.Spellcast.Equip:13: attempt to index global 'player' (a nil value) 23:08:55.581 - Stack Begin 23:08:55.582 - Script 'Players.BiIinear.Backpack.Spellcast.Equip', Line 13 23:08:55.582 - Stack End

01player = game.Players:FindFirstChild(script.Player.Value)
02handle = script.Parent.Handle
03 
04script.Parent.Equipped:Connect(function()
05    handle.Ambience:Play()
06    local fire = handle.Firecast:Clone()
07    fire.Parent = player.Torso
08    fire.Enabled = true
09end)
10 
11script.Parent.Unequipped:Connect(function()
12    handle.Ambience:Pause()
13    player.Torso:FindFirstChild("Firecast"):Destroy()
14end)

Thanks in advance.

2 answers

Log in to vote
0
Answered by 5 years ago

You are not properly finding the CharacterModel. From what I can see, in relation to the script within the tool, the CharacterModel should be script.Parent.Parent, you went one over.

Change line 1 in your top snippet of code to be

1script.Parent.Equip.Player.Value = game.Players:FindFirstChild(script.Parent.Parent.Name)

Then change line 1 on your bottom snippet of code to be

1player = script.Player.Value

The bottom snippet needs a change because the value of "Player" is the actual Player instance. This also means all references to the parts inside of your CharacterModel (based on how you attempted to do so) should be player.Character.BodyPartNameHere

Example being player.Character.Torso to reference torso, however saying Torso assume an R6 model of 'Robloxian'

Ad
Log in to vote
0
Answered by 5 years ago

When a tool is equipped its parent changes from the player backpack to the player's character model. Consider using this as an advantage.

Answer this question