I’m trying to make a turn-based combat system except my script can’t seem to find the “Fight” model in Workspace. In a localscript I clone the arena from replicatedstorage before the enemyturn event fire so it is loaded in, except it still can’t be found.
local rp = game:GetService("ReplicatedStorage")
local enemyturn = rp:WaitForChild("enemyturn")
local enemyattack = rp:WaitForChild("enemyattack")
enemyturn.OnServerEvent:Connect(function()
print("the other thing fired")
local enemy = game.Workspace.Fight.Enemy
print("it knows its in workspace")
local attackvalue = math.random(enemy.minattackvalue, enemy.maxattackvalue)
wait(1.5)
enemyattack:FireAllClients(attackvalue)
end)
How would I make the script find the “Fight” model?
local rp = game:GetService("ReplicatedStorage")
local enemyturn = rp:WaitForChild("enemyturn")
local enemyattack = rp:WaitForChild("enemyattack")
enemyturn.OnServerEvent:Connect(function()
print("the other thing fired")
local enemy = game.Workspace:WaitForChild("Fight").Enemy
print("it knows its in workspace")
local attackvalue = math.random(enemy.minattackvalue, enemy.maxattackvalue)
wait(1.5)
enemyattack:FireAllClients(attackvalue)
end)
Do FindFirstChild, then something that only runs if it returns true, also open the workspace and make sure that item is there
Server scripts don’t need WaitForChild, maybe in a few cases like if you don’t know when the item is added (if you used instance.new or clone something from another script)
The error is on line 7 where it tries to find the enemy and it says there’s an Infinite Yield on “Fight”. Or if I just try to reference it directly it says “Fight is not a valid member of Workspace “Workspace””
local rp = game:GetService("ReplicatedStorage")
local enemyturn = rp:WaitForChild("enemyturn")
local enemyattack = rp:WaitForChild("enemyattack")
enemyturn.OnServerEvent:Connect(function()
print("the other thing fired")
local enemy = game.Workspace:FindFirstChild("Fight").Enemy
if enemy then
print("Found")
local attackvalue = math.random(enemy.minattackvalue, enemy.maxattackvalue)
wait(1.5)
enemyattack:FireAllClients(attackvalue)
else
print("Not found")
end
end)
On line 7 it got an error, “attempted to index nil with ‘Enemy’”. Now I assume it found the model, but not the enemy inside of it. I’ll try to add another FindFirstChild and see if that fixes it
Alright, it clones on a server script right? Also when you start the game press this and it will change to the server
Then check in the workspace for the model
I think I understand now. I cloned it from a localscript. It’s not there on the server side, so I think if I clone it from the serverscript it should work. I forgot that the client-server applied to workspace as well.